How to get UIMenuController work for a custom view?

前端 未结 9 1230
旧时难觅i
旧时难觅i 2020-12-02 10:56

I\'m trying to get the following code work:

UIMenuController * menu = [UIMenuController sharedMenuController];
[menu setTargetRect: CGRectMake(100, 100, 100,         


        
9条回答
  •  清歌不尽
    2020-12-02 11:28

    If you're implementing a custom view and that view is supposed to be the responder (as opposed to some other view, like a UITextField), you need to override the canBecomeFirstResponder function in your view and return YES:

    - (BOOL)canBecomeFirstResponder {
        return YES;
    }
    

    then, when you're displaying the menu, you should do something like the following:

    - (void)myMenuFunc {
        if (![self becomeFirstResponder]) {
            NSLog(@"couldn't become first responder");
            return;
        }
    
        UIMenuController *theMenu = [UIMenuController sharedMenuController];
        CGRect selectionRect = CGRectMake(0, 0, 0, 0);
        [theMenu setTargetRect:selectionRect inView:self];
        [theMenu setMenuVisible:YES animated:YES];
    }
    

提交回复
热议问题