【问题标题】:Can't show more than one option to UIMenuController不能向 UIMenuController 显示多个选项
【发布时间】:2018-10-03 20:54:31
【问题描述】:

我想长按一条消息并在 UIMenuController 中显示两个选项

删除和复制

但只有复制显示

@objc func longPressLabel(sender: AnyObject?) {
   becomeFirstResponder()

   let copyMenuItem = UIMenuItem(title: “Copy”.localized, action: #selector(copyTapped))
   let deleteMenuItem = UIMenuItem(title: “Delete”.localized, action: #selector(deleteTapped))

   let menu = UIMenuController.shared

   menu.arrowDirection = UIMenuControllerArrowDirection.down
   menu.setTargetRect(bounds, in: self)
   menu.menuItems = [copyMenuItem, deleteMenuItem]

   menu.setMenuVisible(true, animated: true)

}

另外,如果我删除所有 UIMenuItems 并创建一个空菜单,这会显示一个复制选项,就像它显示的是模板而不是我添加的选项

我正在使用 IOS 11.1Swift4.1

这是Example

【问题讨论】:

    标签: ios iphone swift swift4 uimenucontroller


    【解决方案1】:

    设置menu.menuItems后需要调用menu.update()更新菜单控制器的外观。

    【讨论】:

    • 更新菜单对我不起作用,我发布了一个解决方案作为答案,谢谢。
    【解决方案2】:

    我添加了这个方法

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
           if action == #selector(deleteTapped(_:)) || action == #selector(copyTapped(_:)) {
               return true
           }
           return false
       }
    

    这有助于说明定义了哪些事件。 如果action是deleteTapped或者Copytapped会返回true并显示出来。

    【讨论】:

      猜你喜欢
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      • 2019-04-11
      • 2018-06-18
      相关资源
      最近更新 更多