class CustomPDFView: PDFView {
var toolMode: ToolMode = .none
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
switch toolMode {
case .comment, .highlight, .translate :
return false
default :
if action == #selector(copy(_:)) || action == #selector(selectAll(_:)) {
return true
}
return false
}
}
}
class originlaView: UIViewController {
override func buildMenu(with builder: UIMenuBuilder) {
super.buildMenu(with: builder)
builder.remove(menu: .share)
//builder.remove(menu: .lookup)
}
}
我清除了“查找”菜单,但“翻译”、“搜索Web”菜单都被清除了。
我想保留“翻译”菜单,但它似乎包含在“查找”菜单中。
我还尝试在“canPerformAction()”中分别清除“lookup”和“searchWeb”,但这不起作用。
以下
buildMenu
的实现将导致“查找”和“搜索网页”菜单不再出现。代码中的注释解释了它是如何工作的。
override func buildMenu(with builder: any UIMenuBuilder) {
// Start by updating the Lookup menu.
// The Lookup menu actually consists of the "Find Selection", "Look Up", and "Translate" commands.
// The "Search Web" menu gets added to the Lookup menu after this buildMenu is called.
// The goal is to show only the "Translate" menu. "Find Selection" is not normally shown so we only need
// to remove the "Look Up" menu and somehow prevent the "Search Web" menu from being added automatically.
// The following code locates the "Translate" command from within the "Lookup" menu. An updated array of
// child commands is returned consisting of just the "Translate" command. As a side effect of returning
// the modified list of children, the "Search Web" menu is no longer added. This achieves the goal of only
// showing the "Translate" menu.
builder.replaceChildren(ofMenu: .lookup) { oldChildren in
// Enumerate the original child commands of the Lookup menu. As of this writing this will include:
// "Find Selection"
// "Look Up"
// "Translate"
for menuElement in oldChildren {
if let command = menuElement as? UICommand {
if command.action == NSSelectorFromString("_translate:") {
// This is the "Translate" command. Return just this command as the updated children of
// the "Lookup" menu.
return [command]
}
}
}
// If the "Translate" command isn't found, return the original children as a fallback. This results
// in the standard menu being shown.
return oldChildren
}
// Also remove the Share menu
builder.remove(menu: .share)
}