UIMenuController为啥不显示呢

首先就是创建

- (void)deleteBtnAction:(UIButton *)deleteBtn
{
    [self becomeFirstResponder];// 用于UIMenuController显示,缺一不可
    UIMenuController *menu = [UIMenuController sharedMenuController];
    UIMenuItem *item1 = [[UIMenuItem alloc] initWithTitle:@"撤销" action:@selector(revokeAction)];
    UIMenuItem *item2 = [[UIMenuItem alloc] initWithTitle:@"确认" action:@selector(sureAction)];
    menu.menuItems = @[item1, item2];
    menu.arrowDirection = UIMenuControllerArrowUp;
    [menu setTargetRect:deleteBtn.frame inView:deleteBtn.superview];//  [menu setTargetRect:所点击的按钮Frame inView:按钮的父视图];
    [menu setMenuVisible:YES animated:YES];
}

用于UIMenuController显示,不可缺少的方法

-(BOOL)canBecomeFirstResponder{
    
    return YES;
 }
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(revokeAction)) {
        return YES;
    }
    if (action == @selector(sureAction)) {
        return YES;
    }
    return NO;//隐藏系统默认的菜单项
}

我们可以获取UIMenuController的通知,从而做出相应的操作

//UIMenuControllerWillShowMenuNotification == 将要显示
//UIMenuControllerDidShowMenuNotification  === 已经显示
//UIMenuControllerWillHideMenuNotification  == 将要隐藏
//UIMenuControllerDidHideMenuNotification == 已经隐藏
//UIMenuControllerMenuFrameDidChangeNotification  == 位置大小改变


 //UIMenuController已经隐藏的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerDidHide) name:UIMenuControllerDidHideMenuNotification object:nil];
//通知回调的方法
- (void)menuControllerDidHide
{
//可以做相应的操作

}

效果图如下

UIMenuController为啥不显示呢_第1张图片
11)效果图.jpg

你可能感兴趣的:(UIMenuController为啥不显示呢)