Problems showing UIMenuController one after another

前端 未结 4 882
鱼传尺愫
鱼传尺愫 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 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.

提交回复
热议问题