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/

相关文章:

objective-c - 文档框架,如 Objective C 的 JavaDoc

ios - 如何计算给定日期后剩余的天,时,分和秒?

iphone - 来自 JSON 的空值

ios - 从另一个 View Controller 导航栏加载 View Controller 的 UIWebView 中的请求 url

ios - 无法在 Apple Calendar 中绑定(bind)()套接字(dylib 注入(inject))

iphone - iOS 6 NSDateFormatter stringFromDate : empty day of week

iphone - iOS Facebook登录快速应用程序切换保持在Facebook App上

ios - 适用于从 iOS 应用程序链接到 iTunes 的规则

ios - 如何以编程方式将 UISegmentedControl 添加到容器 View

iPhone:可以使用 UITextField 清除按钮关闭键盘吗?