How to get UIMenuController work for a custom view?

前端 未结 9 1212
旧时难觅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:26

    // MyView.h
    
    @interface MyView : UIView {
        IBOutlet UITextField * textField_;
    }
    
    @end
    
    // MyView.m
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"show menu");
    
        [textField_ becomeFirstResponder];
        // [self.window becomeFirstResponder];
    
        UIMenuController * menu = [UIMenuController sharedMenuController];
        [menu setTargetRect: CGRectMake(0, 0, 100, 10) inView: self];
        [menu setMenuVisible: YES animated: YES];
    
        NSLog(@"menu width %f, visible %d", menu.menuFrame.size.width, menu.menuVisible);
    }
    
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
        return YES;
    }
    

    You need to add more code in canPerformAction:withSender: - it should check the pasteboard and your selection state. Apple's iPhone Application Programming Guide provide several code snippets.

提交回复
热议问题