我想增加一个下拉菜单,我不知道从哪里开始。苹果的网站引导我学习UIMenu,但我不知道它是如何工作的。
我知道如何制作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];
如何将它附加到UIButton上?
发布于 2022-10-22 12:31:45
根据matt的回答,下面是Objective中的一些示例代码:
// 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
https://stackoverflow.com/questions/68875582
复制