Implementing Custom Controls With UIMenu in UIKit
Recently I was working on a client project with a custom control in a navigation bar to show the associated menu on tap. UIKit supports this behavior out of the box with UIButton
when you associate an UIMenu
and set it’s showsMenuAsPrimaryAction
.
If all you need is a simple button with a menu, use UIButton
by all means. If you want to have this behavior on your custom view it need to extend UIControl
instead of UIView
. Unfortunately there is no .menu
property on UIControl
class.
Another way to showing menus is adding an UIContextMenuInteraction
to your view. But context menu interaction is triggered by long press gesture, and also it adds custom styling to your view while the long press gesture in progress. As the name suggests it is a context menu after all.
UIControl
has a special treatment for UIContextMenuInteraction
. If we extend our custom view from UIControl
and enable context menu interaction via isContextMenuInteractionEnabled
and showsMenuAsPrimaryAction
we can achieve the same behavior as UIButton
has.
Keep in mind UIControl
already conforms to UIContextMenuInteractionDelegate
protocol, and isContextMenuInteractionEnabled
property automatically adds UIContextMenuInteraction
to the control. Make sure to check the delegate protocol for further customizations of the interaction.