首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何呈现下拉菜单?

如何呈现下拉菜单?
EN

Stack Overflow用户
提问于 2021-08-21 18:05:33
回答 1查看 2K关注 0票数 4

我想增加一个下拉菜单,我不知道从哪里开始。苹果的网站引导我学习UIMenu,但我不知道它是如何工作的。

我知道如何制作UIMenu:

代码语言:javascript
运行
复制
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];

如何将它附加到UIButton上?

EN

回答 1

Stack Overflow用户

发布于 2022-10-22 20:31:45

根据matt的回答,下面是Objective中的一些示例代码:

代码语言:javascript
运行
复制
// 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68875582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档