ios - 如何从 iOS 开始显示自定义菜单项?

标签 ios objective-c iphone

当我选择 WebView 内容时,如何删除默认菜单项,如复制、过去、全选。如何将自定义操作放在默认菜单项的中间。我把这些放在最后显示的项目,我想从头开始我的自定义操作。 我在 View 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;

请帮帮我。

最佳答案

enter image description here正如这里的回答 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
{}

关于ios - 如何从 iOS 开始显示自定义菜单项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35909897/

相关文章:

ios - Google map 集成 iOS 1.9.0 问题

ios - 与 UITapGestureRecognizer 一起使用时,UISwipeGestureRecognizer 未注册滑动

ios - 带有自签名证书的 HTTPS iOS

iphone - 链接自动打开我的应用程序?

ios - 自定义 Collection View 单元格的重写方法不调用

ios - 如何在iOS中的动画图像上放置按钮?

ios - 使用 Restkit 0.20 映射此类不符合 key 的键值编码

iphone - 如何更新 2D iPhone 游戏以兼容 iPhone 4 的视网膜显示屏?

ios - 检查自定义键盘中 documentContextBeforeInput 中的字符

ios - 在 Mobile Safari 上缩放 iframe 会破坏 iframe 的链接