How to modify UIMenu for UIBarButtonItem and UIButton
One solution I found is to store a reference to your bar button item or button and recreate the menu each time any state changes that affects the available actions in the menu. menu
is settable property so it can be changed any time after the button is created. You can also get the current menu and replace its children like so: button.menu = button.menu?.replacingChildren([])
For scenarios where you are not informed when the state changes, you really need to be able to update the menu right before it appears. For UIButton, you could change the menu when the user touches down on the button via target/action like so: button.addTarget(self, action: #selector(buttonTouchedDown(_:)), for: .touchDown)
. I confirmed this works with iOS 14 beta at least at this time, but only if showsMenuAsPrimaryAction
is false so they have to long press to open the menu. I haven't found a solution for UIBarButtonItem, but you could use a UIButton as a custom view for the UIBarButtonItem.
These don't feel like great solutions, will update if I find something better.