
如何为WKWebView中的链接设置自定义上下文菜单?
您可以通过执行以下操作在项目上设置上下文菜单:
let interaction = UIContextMenuInteraction(delegate: self)
someItem.addInteraction(interaction)并添加UIContextMenuInteractionDelegate委托:
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中的链接时,如何使用自定义上下文菜单?
发布于 2020-10-13 18:21:24
您应该为您的WKWebView实现contextMenuConfigurationForElement UI委托方法,例如:
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)
}
}https://stackoverflow.com/questions/64273650
复制相似问题