Problems showing UIMenuController one after another

♀尐吖头ヾ 提交于 2020-01-01 02:34:09

问题


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 the reference to the shared UIMenuController, setting my NSArray of UIMenuItems into the menuItems, and everything work fine as long as I add a single item. For instance I see [COPY|FOOBAR].

Instead if I try adding more than a single item, what happen is that I see [COPY|MORE], if I press into MORE than finally the other items will show up.

Is possible to show directly [COPY|FOO|BAR|THREE|FOUR] instead? I saw a few applications that are able to do this, notably iBooks.

Any help very appreaciated, thank you.

Cheers, sissensio


回答1:


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;
}



回答2:


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




回答3:


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.




回答4:


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

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];

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



来源:https://stackoverflow.com/questions/3255070/problems-showing-uimenucontroller-one-after-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!