【问题标题】:How to display a next set of options in UIMenuController如何在 UIMenuController 中显示下一组选项
【发布时间】:2019-04-02 00:33:15
【问题描述】:

如果您在 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)
  }
}

【问题讨论】:

    标签: ios uimenucontroller


    【解决方案1】:

    不要调用update,而是调用setMenuVisible(:,animated)

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

    这将产生您正在寻找的效果:

    【讨论】:

      猜你喜欢
      • 2018-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      相关资源
      最近更新 更多