首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为WKWebView链接设置UIContextMenu

为WKWebView链接设置UIContextMenu
EN

Stack Overflow用户
提问于 2020-10-09 11:45:49
回答 1查看 214关注 0票数 1

如何为WKWebView中的链接设置自定义上下文菜单?

您可以通过执行以下操作在项目上设置上下文菜单:

代码语言:javascript
复制
    let interaction = UIContextMenuInteraction(delegate: self)
    someItem.addInteraction(interaction)

并添加UIContextMenuInteractionDelegate委托:

代码语言:javascript
复制
 func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { (_) -> UIMenu? in
            let shareAction = UIAction(title: "Send to Friend", image: UIImage(systemName: "square.and.arrow.up")) { _ in
                // Pressed
            }
            let menu = UIMenu(title: "", children: [shareAction])
            return menu
        }
        return configuration
    }

当用户按住WKWebView中的链接时,如何使用自定义上下文菜单?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-13 18:21:24

您应该为您的WKWebView实现contextMenuConfigurationForElement UI委托方法,例如:

代码语言:javascript
复制
override func viewDidLoad() {
    ...
    webView?.uiDelegate = self
}

extension ViewController : WKUIDelegate {
    
    func webView(_ webView: WKWebView, contextMenuConfigurationForElement elementInfo: WKContextMenuElementInfo, completionHandler: @escaping (UIContextMenuConfiguration?) -> Void) {
        let share = UIAction(title: "Send to Friend") { _ in print("Send to Friend") }
        let configuration = UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
          UIMenu(title: "Actions", children: [share])
        }
        completionHandler(configuration)
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64273650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档