当我选择WebView内容时,如何删除默认菜单项,例如复制,过去,SelectAll。如何在默认菜单项的中间放置自定义操作。我将这些显示在最后一个菜单项中,我想从一开始就执行我的自定义操作。
我在视图didAppear方法中使用以下代码。

UIMenuItem *customMenuItem1=[[UIMenuItem alloc] initWithTitle:@"Highlight" action:@selector(customAction1:)];
UIMenuItem *customMenuItem2=[[UIMenuItem alloc] initWithTitle:@"UnHighlight" action:@selector(UnHighlighted:)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:customMenuItem1,customMenuItem2,nil]];
[UIMenuController sharedMenuController].menuVisible=YES;

请帮我。

最佳答案

ios - 如何从iOS开始显示自定义菜单项?-LMLPHP如此处回答showing custom menu on selection in UIWebView in iphone

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com"]]];
    self.webview.backgroundColor = [UIColor blueColor];
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UIMenuItem *customMenuItem1 = [[UIMenuItem alloc] initWithTitle:@"Custom 1" action:@selector(customAction1:)];
    UIMenuItem *customMenuItem2 = [[UIMenuItem alloc] initWithTitle:@"Custom 2" action:@selector(customAction2:)];
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:customMenuItem1, customMenuItem2, nil]];

}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[UIMenuController sharedMenuController] setMenuItems:nil];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (self.webview.superview != nil)// && ![urlTextField isFirstResponder])
    {
        if (action == @selector(customAction1:) || action == @selector(customAction2:))
        {
            return YES;
        }
    }
    return [super canPerformAction:action withSender:sender];
}
-(void)customAction1:(UIMenuItem*)item
{}
-(void)customAction2:(UIMenuItem*)item
{}

10-08 05:30
查看更多