在 iOS 14 中,有新的 API UIMenu
,现在可以附加到UIBarButtonItem
,就像这样:
这是我的代码:
@IBOutlet weak var addButton: UIBarButtonItem! // The button is from the storyboard.
override func viewDidAppear(_ animated: Bool) {
if #available(iOS 14.0, *) {
let simpleAction : UIAction = .init(title: "Simple", image: nil, identifier: nil, discoverabilityTitle: nil, attributes: .init(), state: .mixed, handler: { (action) in
self.addButtonActionPressed(action: .simple)
})
let advancedAction : UIAction = .init(title: "Advanced", image: nil, identifier: nil, discoverabilityTitle: nil, attributes: .init(), state: .mixed, handler: { (action) in
self.addButtonActionPressed(action: .advanced)
})
let actions = [simpleAction, advancedAction]
let menu = UIMenu(title: "", image: nil, identifier: nil, options: .displayInline, children: actions)
addButton.primaryAction = nil
addButton.menu = menu
}
}
但问题是,当我按下按钮时,什么也没有发生。只有当我长按按钮时,它才会显示菜单。我在网上看到过这段代码:
button.showsMenuAsPrimaryAction = true
但这对我没有帮助,因为Value of type 'UIBarButtonItem' has no member 'showsMenuAsPrimaryAction'
任何想法如何解决?我正在使用 Xcode 12.0 beta 4 (12A8179i)。