UIButton 和临时 UIMenu

问题描述

是否可以根据外部条件更改 UIButton 是否具有 UIMenu?

let infoButton = UIButton()
infoButton.showsMenuAsPrimaryAction = true
infoButton.menu = UIMenu(options: .displayInline,children: [])
infoButton.addAction(UIAction { [weak infoButton] (action) in
   infoButton?.menu = infoButton?.menu?.replacingChildren([new items go here...])
},for: .menuActionTriggered)

有没有办法阻止该菜单,但在某些条件下触发 TouchUpInside 类型的控件事件?

解决方法

如果您不想在点击按钮时出现菜单,则不应将 showsMenuAsPrimaryAction 设置为 true。该值意味着目标操作中的修饰(如果有)将被忽略,而是出现菜单。