How to get UIMenuController work for a custom view

2019-01-10 09:08发布

I'm trying to get the following code work:

UIMenuController * menu = [UIMenuController sharedMenuController];
[menu setTargetRect: CGRectMake(100, 100, 100, 100) inView: self.view];
[menu setMenuVisible: YES animated: YES];

The menu instance is ready but it doesn't show - the width is always zero.

Or is there some sample code on this UIPasteboard/UIMenuController topic?

9条回答
我只想做你的唯一
2楼-- · 2019-01-10 09:31

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];
}
查看更多
Rolldiameter
3楼-- · 2019-01-10 09:32

I think Cam is right, need override both canPerformAction and canBecomeFirstResponder

- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(doSomething:)) {
        return YES;
    }
    return NO;
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}
查看更多
做个烂人
4楼-- · 2019-01-10 09:33

to display UIMenuController one has to add following

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(cut:))
        return NO;
    else if (action == @selector(copy:))
        return YES;
    else if (action == @selector(paste:))
        return NO;
    else if (action == @selector(select:) || action == @selector(selectAll:))
        return NO;
    else
        return [super canPerformAction:action withSender:sender];
}
查看更多
Animai°情兽
5楼-- · 2019-01-10 09:44

UIMenuController doesn't have a view. I just searched some code from apple's iPhone Application Programming Guide: Event Handling:

Listing 3-4 Displaying the editing menu

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *theTouch = [touches anyObject];

    if ([theTouch tapCount] == 2  && [self becomeFirstResponder]) {

        // selection management code goes here...

        // bring up editing menu.
        UIMenuController *theMenu = [UIMenuController sharedMenuController];
        CGRect selectionRect = CGRectMake(currentSelection.x, currentSelection.y, SIDE, SIDE);
        [theMenu setTargetRect:selectionRect inView:self];
        [theMenu setMenuVisible:YES animated:YES];
    }
}
查看更多
ら.Afraid
6楼-- · 2019-01-10 09:48

Don't you have to add the UIMenuController* menu to the main or subview, E.G. self.view? I think it's something like [self.view addSubView:menu.view]; Or am I missing the point of your question. You might also want to set the frame of the menu's view.

查看更多
The star\"
7楼-- · 2019-01-10 09:55

In case somebody still has problems: My menu used to work and some day stopped working miraculously. Everything else in my app still worked. Now I had removed the [window makeKeyAndVisible] method from application:didFinishLaunchingWithOptions: method, and while everything else still worked, this breaks UIMenuController!

Stupid error on my side, difficult to find the culprit...

查看更多
登录 后发表回答