我想禁用标注菜单,如果您长时间点击web视图中的元素,则会弹出该菜单:

像this one这样的答案有很多,但似乎都不起作用。不知道用于UIWebView的内容是否也适用于WKWebView ...
我试着通过JavaScript操作CSS。这似乎只有在您将脚本添加到WKUserContentController而不是在didFinish()上时才能起作用。
不起作用的东西:
window.getSelection().removeAllRanges();
document.body.style.webkitTouchCallout='none';
document.body.style.webkitUserSelect='none';部分起作用的东西:
var style = document.createElement('style');
style.innerHTML = '* { -webkit-touch-callout: none; -webkit-user-select: none; }';
document.getElementsByTagName('head')[0].appendChild(style);或在CSS表单中
*:not(input):not(textarea) {
-webkit-user-select: none; /* disable selection/copy of UIWebView */
-webkit-touch-callout: none; /* disable the iOS popup when long-press on a link */
}似乎只有像*或*:not(input):not(textarea)这样的东西才能工作(没有body或其他特定的标签)。这样做的问题是,有太多的元素通过这个被禁用。我只需要在一些特定的元素上!
我还尝试使用canPerformAction()
private static readonly Selector copyAction = new Selector("copy:");
private static readonly Selector pasteAction = new Selector("paste:");
private static readonly Selector cutAction = new Selector("cut:");
public override bool CanPerform(Selector action, NSObject withSender)
{
if (action == copyAction || action == pasteAction || action == cutAction)
{
System.Diagnostics.Debug.WriteLine(action.Name);
}
return false;
}在这里,似乎链中的另一个响应器返回true并覆盖我的设置。弹出/注解菜单仍会出现。我只是想最小化可用选项(如上面的屏幕所示)。
我唯一可以尝试的就是使用手势识别器来禁用这种点击,但目前我还不知道该怎么做。
要禁用弹出/标注菜单,我该怎么做?
发布于 2021-09-22 16:48:44
我曾尝试覆盖在UIWebView中运行良好的canPerformAction方法,但它在WKWebView上不起作用。
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.cut(_:)) ||
action == #selector(UIResponderStandardEditActions.copy(_:)) ||
action == #selector(UIResponderStandardEditActions.selectAll(_:)) ||
action == #selector(UIResponderStandardEditActions.select(_:)) {
return false
}
return super.canPerformAction(action, withSender: sender)
}https://stackoverflow.com/questions/66799328
复制相似问题