如何在UIMenuController中显示下一组选项

时间:2018-10-29 05:40:27

标签: ios uimenucontroller

如果您在iOS上突出显示一个单词,Apple会在UIMenuController中显示一个“替换...”选项。点击后,菜单控制器将其选项集更改为替换词:

enter image description here

选择选项后,如何实现更新项目的相同行为?


我尝试调用UIMenuController。update(),但是选择第一个选项后,菜单消失了。我必须再次点击以显示菜单。

enter image description here

我已经知道如何自定义菜单项并添加自己的菜单项。

我不知道如何在选择一个选项后显示菜单。我尝试执行以下操作:

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)
  }
}

1 个答案:

答案 0 :(得分:0)

致电update而不是致电setMenuVisible(:,animated)

UIMenuController.shared.setMenuVisible(true, animated: true)

这将产生您想要的效果:

enter image description here