I wish to current a menu from a UIBarButtonItem
, however provided that a runtime examine succeeds when the button is tapped, in any other case present an alert.
Fast background. I had some older code (pre-UIMenu days) that dealt with the UIBarButtonItem
with a goal/motion that will carry out the examine after which both present an alert or current an motion sheet.
I am attempting to replace that code to make use of a UIMenu
as a substitute of an motion sheet (UIAlertController
). I understand how to create the UIBarButtonItem
with a UIMenu
. That is straightforward to implement.
What I am unable to discover in any APIs or in any looking right here on SO, is the right way to manually show a UIMenu
.
This is a tough instance of my code that straight reveals a menu from the UIBarButtonItem
:
btnAdd = UIBarButtonItem(systemItem: .add, menu: UIMenu(kids: [
// An array of UIAction instances for each menu item
]))
That code works simply advantageous however I want to alter it so the menu solely seems below the suitable situation. I am pondering of one thing like the next however I do not know the right way to write the road of code that manually shows a UIMenu
.
btnAdd = UIBarButtonItem(barButtonSystemItem: .add, goal: self, motion: #selector(addAction))
...
@objc func addAction(_ sender: UIBarButtonItem) {
if someRuntimeCondition == true {
let menu = UIMenu(kids: [
// An array of UIAction instances for each menu item
])
??? // show menu from sender?
} else {
// Create and show an alert
}
}
I really feel like I am lacking one thing easy and apparent however I simply do not see it.
I’ve reviewed the documentation for UIMenu
, UIBarButtonItem
, UIContextMenuInteraction
, and UIMenuController
(deprecated). None of those appear to supply a option to manually show a menu from a UIBarButtonItem
. I’ve additionally checked out a few Apple’s pattern apps.
Any answer must work with iOS 15.0+.