如果您在iOS上突出显示一个单词,Apple会在UIMenuController中显示一个“替换...”选项。点击后,菜单控制器将其选项集更改为替换词:
选择选项后,如何实现更新项目的相同行为?
我尝试调用UIMenuController。update()
,但是选择第一个选项后,菜单消失了。我必须再次点击以显示菜单。
我已经知道如何自定义菜单项并添加自己的菜单项。
我不知道如何在选择一个选项后显示菜单。我尝试执行以下操作:
class MyTextView: UITextView {
/// called once on viewDidLoad
func initializeMenuItems() {
UIMenuController.shared.menuItems = [
UIMenuItem(title: "Thesaurus...", action: #selector(thesaurusTapped)),
]
}
@objc
private func thesaurusTapped(_ sender: UIMenuController) {
UIMenuController.shared.menuItems = [
UIMenuItem(title: "Experiment", action: #selector(replacementTapped)),
UIMenuItem(title: "Trial", action: #selector(replacementTapped)),
UIMenuItem(title: "Inspection", action: #selector(replacementTapped)),
]
UIMenuController.shared.update()
}
@objc
private func replacementTapped(_ sender: UIMenuController) {
// TODO: Display the option the user chose
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return action == #selector(replacementTapped) || action == #selector(thesaurusTapped)
}
}