创建自定义上下文菜单

创建自定义上下文菜单

本文介绍了UIWebView - 创建自定义上下文菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了UIWebView控件到我的应用程序。

I added UIWebView control to my application.

要禁用默认上下文菜单,我实现了webViewDidFinishLoad。

To disable default context menu, I implemented webViewDidFinishLoad.

- (void) webViewDidFinishLoad:(UIWebView *)theWebView {
    NSString *varMySheet = @"var mySheet = document.styleSheet[0];";
    NSString *addCSSRule = @"function addCSSRule(selector, newRule) {"
        "if (mySheet.addRule) {"
        "mySheet.addRule(selector, newRule);"
        "} else {"
        "ruleIndex = mySheet.cssRules.length;"
        "mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex;"
        "}"
        "}";
    ...
    NSString *insertRule = @"addCSSRule('body', '-webkit-touch-callout: none;')";

    [webView stringByEvaluatingJavaScriptFromString:varMySheet];
    [webView stringByEvaluatingJavaScriptFromString:addCSSRule];
    [webView stringByEvaluatingJavaScriptFromString:insertRule];
    ...
}

但是webview的上下文菜单并没有消失。任何人帮助我。

But context menu of webview doesn't disappear. Anyone help me.

我也试过了

[webView stringByEvaluatingJavaScriptFromString:@"document.body.style.webkitTouchCallout='none';"];

它没有工作。
谢谢。

It didn't work.Thanks.

推荐答案

需要对UIWebView进行子类化在你的自定义视图中,只是实现方法canPerformAction:withSender这样:

You just need to subclass the UIWebView. In your custom view, just implement the method canPerformAction:withSender like this:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  return NO;
}

那么所有菜单项都会消失。如果你只想显示一些项目,你应该为指定的项目返回YES。

then all menu items will disappear. If you just wanna show some of items, you should return YES for the specified item.

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  BOOL ret = NO;
  if (action == @selector(copy:)) ret = YES;
  return ret;
}

当你长时间按下视图。

这篇关于UIWebView - 创建自定义上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:23