Problems showing UIMenuController one after another

前端 未结 4 881
鱼传尺愫
鱼传尺愫 2021-02-08 10:50

I\'m using the new customization abilities of the UIMenuController to add things other than \"Copy\" to the menu for cut&paste into a webview.

What I do is getting t

相关标签:
4条回答
  • 2021-02-08 10:59

    fluXa's answer is actually correct, but I dont think it was very clear.

    The issue is that when adding custom UIMenuItem objects to the shared menu controller ([UIMenuController sharedMenuController]), only the first custom UIMenuItem will be shown on the initial display of the menu. The remaining custom menu items will be shown if the user taps "More...".

    However, if the menu doesn't include any built-in system menu items (copy:, paste:, etc), the initial menu display will show all custom menu items and no "More..." item.

    If you need to include the built-in system items, simply add custom UIMenuItems having the same title but with a different selector. ( myCopy: vs. copy: )

    Essentially it boils down to NOT calling the default implementation of canPerformAction:withSender:, explicitly handling all custom menu items, and returning NO for all other (system-supplied) menu items:

    - (BOOL) canPerformAction:(SEL)action withSender:(id)sender
    {
        if ( action == @selector( onCommand1: ) )
        {
            // logic for showing/hiding command1
            BOOL show = ...;
            return show;
        }
    
        if ( action == @selector( onCommand2: ) )
        {
            // logic for showing/hiding command2
            BOOL show = ...;
            return show;
        }
    
        if ( action == @selector( onCopy: ) )
        {
            // always show our custom "copy" command
            return YES;
        }   
    
        return NO;
    }
    
    0 讨论(0)
  • 2021-02-08 11:00

    you can get the rect of the previously displayed UIMenuController using menuFrame (readonly property), using that you can calculate the position for another UIMenuController to be shown at the same place.

    In the action method where you are about to show the second UIMenuController, get the frame of the previous UIMenuController

    enter image description here

    CGRect previousRect = [[UIMenuController sharedMenuController] menuFrame];
    
    CGRect newRect = CGRectMake(previousRect.origin.x + previousRect.size.width/2,   previousRect.origin.y + previousRect.size.height, 0, 0);
    

    Roughly you will get the arrow position. Now show the second UIMenuController

    UIMenuItem *testMenuItem1 = [[UIMenuItem alloc] initWithTitle:@"test1" action:@selector(test1ItemClicked)];
    UIMenuItem *testMenuItem2 = [[UIMenuItem alloc] initWithTitle:@"test2" action:@selector(test2ItemClicked)];
    
    
    [[UIMenuController sharedMenuController] setMenuItems:@[testMenuItem1,testMenuItem2]];
    
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    
    [menuController setTargetRect:newRect inView:_readerWebView];
    
    [menuController setMenuVisible:YES animated:YES];
    

    enter image description here

    since UIMenuController is a singleton, if you want to show the previous menuItems, again you have to set them.

    0 讨论(0)
  • 2021-02-08 11:06

    Ran into the same problem and what I did was override the webview with a subclass (yep I know you shouldn't) and return NO for canPerformAction: for the copy: selector. Then I added my own Copy item to the ShareMenuController that calls the original method from UIWebview. That way as many items as you want can be added and are initially visible.

    0 讨论(0)
  • 2021-02-08 11:13

    we have the same problem actually when i tried to develop an application in iPad. But what i did is i disabled the popup menu items in

    • (BOOL)canPerformAction:(SEL)action withSender:(id)sender

    Using

    if ( [UIMenuController sharedMenuController] ) { [UIMenuController sharedMenuController].menuVisible = NO; } return NO;

    Then i used a UIPopoverController.

    Regards, ZaldzBugz

    0 讨论(0)
提交回复
热议问题