【问题标题】:How to modify UIMenu before it's shown to support dynamic actions如何修改 UIBarButtonItem 和 UIButton 的 UIMenu
【发布时间】:2020-11-08 05:43:23
【问题描述】:

iOS 14 添加了在点击或长按 UIBarButtonItem 或 UIButton 时显示菜单的功能,如下所示:

let menu = UIMenu(children: [UIAction(title: "Action", image: nil) { action in
    //do something
}])
button.menu = menu
barButtonItem = UIBarButtonItem(title: "Show Menu", image: nil, primaryAction: nil, menu: menu)

这通常会替换操作表(UIAlertControlleractionSheet 样式)。拥有一个动态操作表非常常见,其中仅包含操作或根据用户点击按钮时的某些状态可能会禁用操作。但是使用此 API,菜单是在创建按钮时创建的。如何在菜单出现之前对其进行修改或以其他方式使其动态化以确保适当的操作可用并在出现时处于适当的状态?

【问题讨论】:

    标签: ios uikit ios14 uimenu


    【解决方案1】:

    通过UIBarButtonItem 找到了该案例的解决方案。我的解决方案基于 Jordan H 解决方案,但我面临一个错误 - 我的菜单更新方法 regenerateContextMenu() 在每次出现菜单时都没有被调用,并且我在菜单中获得了不相关的数据。所以我稍微改了一下代码:

    private lazy var threePointBttn: UIButton = {
        $0.setImage(UIImage(systemName: "ellipsis"), for: .normal)
        // pay attention on UIControl.Event in next line
        $0.addTarget(self, action: #selector(regenerateContextMenu), for: .menuActionTriggered)
        $0.showsMenuAsPrimaryAction = true
        return $0
    }(UIButton(type: .system))
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        threePointBttn.menu = createContextMenu()
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: threePointBttn)
    }
    
    private func createContextMenu() -> UIMenu {
        let action1 = UIAction(title:...
        // ...
        return UIMenu(title: "Some title", children: [action1, action2...])
    }
    
    @objc private func regenerateContextMenu() {
        threePointBttn.menu = createContextMenu()
    }
    

    在 iOS 14.7.1 上测试

    【讨论】:

      【解决方案2】:

      经过一番尝试,我发现您可以通过将menu属性设置为null然后设置新的UIIMenu来修改UIButton.menu

      这是我制作的示例代码

      @IBOutlet weak var button: UIButton!
      
      func generateMenu(max: Int, isRandom: Bool = false) -> UIMenu {
          let n = isRandom ? Int.random(in: 1...max) : max
          print("GENERATED MENU: \(n)")
          let items = (0..<n).compactMap { i -> UIAction in
              UIAction(
                  title: "Menu \(i)",
                  image: nil
              ) {[weak self] _ in
                  guard let self = self else { return }
      
                  self.button.menu = nil // HERE
      
                  self.button.menu = self.generateMenu(max: 10, isRandom: true)
                  print("Tap")
              }
          }
       
          let m = UIMenu(
              title: "Test", image: nil,
              identifier: UIMenu.Identifier(rawValue: "Hello.menu"),
              options: .displayInline, children: items)
          return m
      }
      
      override func viewDidLoad() {
          super.viewDidLoad()
          button.menu = generateMenu(max: 10)
          button.showsMenuAsPrimaryAction = true
      }
      

      【讨论】:

        【解决方案3】:

        我发现的一个解决方案是存储对条形按钮项或按钮的引用,并在每次影响菜单中可用操作的任何状态更改时重新创建菜单。 menu 是可设置的属性,因此可以在创建按钮后随时更改。您还可以像这样获取当前菜单并替换其子项:button.menu = button.menu?.replacingChildren([])

        对于在状态更改时未通知您的情况,您确实需要能够在菜单出现之前对其进行更新。对于 UIButton,您可以在用户通过目标/操作按下按钮时更改菜单,如下所示:button.addTarget(self, action: #selector(buttonTouchedDown(_:)), for: .touchDown)。我确认这至少适用于 iOS 14 beta,但前提是 showsMenuAsPrimaryAction 为假,因此他们必须长按才能打开菜单。我还没有找到 UIBarButtonItem 的解决方案,但您可以使用 UIButton 作为 UIBarButtonItem 的自定义视图。

        这些感觉不是很好的解决方案,如果我找到更好的解决方案,会更新。

        【讨论】:

        • 是的。 UIBarButtonItem 没有解决方案。我没有找到办法,检测UIBarButtonItem按下,然后动态构建菜单并显示动态菜单。
        猜你喜欢
        • 2021-06-11
        • 2020-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-17
        • 2018-11-28
        相关资源
        最近更新 更多