Present UIMenu programatically

Is it possible to present UIMenu programmatically in UIButton or UIBarButton? I have custom logic where I'd like to show that menu.

I couldn't find any solution.
to present UIMenu programmatically in UIButton

What do you mean ?
  • Open a UIMenu when you tap the button ?

  • Have a UIMenu inside the UIButton ?

I don't think the latter is possible. Button can have Text, attributed text, an image, but no more.
For the former, you just do it in the IBAction. Is it where you have a problem ? Could you describe precisely ?
Hello Claude,
I'd like to open a UIMenu when you tap the button but trigger this event programatically.
That's now possible in iOS 14.

Code Block
let destruct = UIAction(title: "Destruct", attributes: .destructive) { _ in } 2
let items = UIMenu(title: "More", options: .displayInline, children: [
UIAction(title: "Item 1", image: UIImage(systemName: "mic"), handler: { _ in }),
UIAction(title: "Item 2", image: UIImage(systemName: "envelope"), handler: { _ in })
UIAction(title: "Item 3", image: UIImage(systemName: "flame.fill"), handler: { _ in
UIAction(title: "Item 4", image: UIImage(systemName: "video"), state: .on, handler:
])
button.menu = UIMenu(title: "", children: [items, destruct])


Get full tutorial here:
https ://medium .com/better-programming/whats-new-in-ios-14s-uimenu-and-contextmenu-433cd2037c37
Hello Claude,
thanks for your reply.
Not sure if you understand my question, but I want to achieve opening the UIMenu programatically. The only way I'm aware is by tapping the button physically with a finger. I want to open the UIMenu programmatically in the code.
Hey  VabererP. Did you find the way hot described behaviour could be done?

I have the same question

In iOS 16 (and possibly prior), you can use UIAlertController with preferredStyle set to .actionSheet. Create a UIAlertAction for each option and add each action to the UIAlertController object. Then you can present the UIAlertController like you might with any other view controller with UIViewController.present(...)

As per the example below you can cause the selection action to call back to the object that configures the alert to notify of the selection.

 let option1 = UIAlertAction(title: "Option 1", style: .default) { [weak self] _ in
                    self?.handleOptionSelection("Option 1")
               }

Although UIAlertController is a subclass of UIViewController, there are limitations about what you can do if you use that class however, as Apple says it's not designed for subclassing nor are its private methods intended to be used by apps. If you need to customize the appearance quite a bit, you may have to do some more leg work and use a custom view controller for the pop-up content and a UIPresentationController to handle the presentation and dismissal.

Did anyone ever find a solution for this? I'd like to present a UIMenu from a UIView as a result of a gesture recognizer firing... is that possible?

Present UIMenu programatically
 
 
Q