4

I'm looking to add a pull-down menu and I have no idea where to start. Apple's website guides me to UIMenu but I can't figure out how it works.

I know how to make a UIMenu:

NSMutableArray* actions = [[NSMutableArray alloc] init];

[actions addObject:[UIAction actionWithTitle:@"Edit"
                                       image:nil
                                  identifier:nil
                                     handler:^(__kindof UIAction* _Nonnull action) {
    
    // ...
}]];

UIMenu* menu =
[UIMenu menuWithTitle:@""
             children:actions];

How do I attach it to a UIButton?

Vulkan
  • 1,004
  • 16
  • 44
  • 1
    "I can't find the menu builder"... https://developer.apple.com/documentation/uikit/uimenubuilder?language=objc – matt Aug 21 '21 at 18:30
  • There's no UIMenuBuilder class or object in Xcode I don't know where to access it. Even if I knew I don't know how to present the menu. – Vulkan Aug 21 '21 at 18:41
  • 1
    Did you read the page I linked you to? It answers both of those questions directly, right there on the page. – matt Aug 21 '21 at 19:07
  • You are supposed to override this function: buildMenuWithBuilder but I have no idea where. I'm not trying to get spoon fed. – Vulkan Aug 21 '21 at 19:11
  • Maybe I've messed things up. This is what I'm trying to achieve and I can only find swift code: https://medium.com/ivymobility-developers/context-menus-in-ios-226727a8aa88 – Vulkan Aug 21 '21 at 19:13
  • 1
    But that's not a system menu. It's a context menu. You don't use a builder for that. – matt Aug 21 '21 at 19:17
  • 1
    Did you want this menu to appear in response to pressing on a view? Then you give that view a UIContextMenuInteraction. Just like in the tutorial you're pointing to. You have given no information as to how _you_ expect _your_ menu to appear, so it's impossible to say more. – matt Aug 21 '21 at 19:20
  • OK, I figured context menus and added the solution to the main post. This is what I was actually aiming for but I didn't know what was its name: https://developer.apple.com/design/human-interface-guidelines/ios/controls/pull-down-menus/ – Vulkan Aug 21 '21 at 19:44
  • If you scroll a bit you 'll see it refers to UIMenu. What!? – Vulkan Aug 21 '21 at 19:45
  • 1
    Very confusing. Now you've got some code and no question. What's the question at this point? Was that supposed to be the answer? Then don't put it in the question, put it in an answer. – matt Aug 21 '21 at 19:47
  • I was looking for a context menu. I found the context menu. Turns out I was looking for a pull-down menu. These two look almost the same. I will use the context menu anyway for some other interactions. – Vulkan Aug 21 '21 at 19:50
  • 1
    So what's the question? – matt Aug 21 '21 at 20:32
  • It sounds like a wild goose chase. – El Tomato Aug 21 '21 at 20:56
  • The question is: "How do I present a pull-down menu?" – Vulkan Aug 21 '21 at 20:58
  • 1
    What _is_ a "pull-down menu" in your terminology? Do you mean a button where a menu appears when you long-press the button? If so, a UIButton has a `menu`; you assign a UIMenu to it. https://developer.apple.com/documentation/uikit/uibutton/3601189-menu – matt Aug 21 '21 at 21:33
  • It's not my terminology, it's how Apple named it: https://developer.apple.com/design/human-interface-guidelines/ios/controls/pull-down-menus/ – Vulkan Aug 21 '21 at 22:02
  • Not long press, just a single tap. – Vulkan Aug 21 '21 at 22:56

2 Answers2

4

So it seems, after several radical rewrites of your question, that you want a menu that appears from a button. Well, a UIButton has a menu property and you assign a UIMenu to it. Done.

https://developer.apple.com/documentation/uikit/uibutton/3601189-menu?language=objc

If you also want the menu to appear as a response to a simple tap, rather than a long press, then also set the button's showsMenuAsPrimaryAction property to YES.

https://developer.apple.com/documentation/uikit/uicontrol/3601223-showsmenuasprimaryaction?language=objc

(A UIBarButtonItem has similar properties, in case you want to make the menu appear that way.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

Following up on matt's answer, here is some example code in Objective-C:

// Add a UIMenu (with three actions) to a UIButton
NSMutableArray  *theMenuActions = [[NSMutableArray alloc] initWithCapacity:3];

[theMenuActions addObject:[UIAction actionWithTitle:NSLocalizedString(@"FullName", @"")
                                              image:nil
                                         identifier:@"search_scope_full"
                                            handler:^(__kindof UIAction* _Nonnull action) {
self.mySearchScope  = @"full";
}]];
    
[theMenuActions addObject:[UIAction actionWithTitle:NSLocalizedString(@"FirstName", @"")
                                              image:nil
                                         identifier:@"search_scope_first"
                                            handler:^(__kindof UIAction* _Nonnull action) {
self.mySearchScope  = @"first";
}]];

[theMenuActions addObject:[UIAction actionWithTitle:NSLocalizedString(@"LastName", @"")
                                              image:nil
                                         identifier:@"search_scope_last"
                                            handler:^(__kindof UIAction* _Nonnull action) {
self.mySearchScope  = @"last";
}]];

// searchScopeBtn is a UIButton
self.searchScopeBtn.menu = [UIMenu menuWithTitle:NSLocalizedString(@"SearchScope", @"") 
                                        children:theMenuActions];
self.searchScopeBtn.showsMenuAsPrimaryAction = YES;

// When the UIButton is tapped, the UIMenu will appear
lifjoy
  • 2,158
  • 21
  • 19