The answers from @pointum and @zhoudu aren't supported on Mac Catalyst apps, so here's an option for that.
According to this answer, the search field is added by macOS. I looked for a plist key to disable it, but didn't find anything. Then I started messing with the buildMenuWithBuilder
method. I had to change my AppDelegate
class to be a subclass of UIResponder
instead of NSObject
and then I could override that method in my AppDelegate
. Once that was running, it took a few tries to do something useful with it.
Attempt 1: I tried removing the first element of the Help menu, which was the search field.
- (void)buildMenuWithBuilder:(id<UIMenuBuilder>)builder {
[super buildMenuWithBuilder:builder];
[builder replaceChildrenOfMenuForIdentifier:UIMenuHelp fromChildrenBlock:^(NSArray<UIMenuElement *> *currentChildren) {
NSMutableArray *newChildren = [NSMutableArray arrayWithArray:currentChildren];
[newChildren removeObjectAtIndex:0 withSafeChecks:TRUE];
return newChildren;
}];
}
But the search field is added after this method runs, so this code only removed the "[app name] Help" element that I wanted to keep.
Attempt 2: I tried taking the "[app name] Help" menu element from the default menu, then adding it to a new menu and replacing the default help menu with that:
- (void)buildMenuWithBuilder:(id<UIMenuBuilder>)builder {
[super buildMenuWithBuilder:builder];
UIMenuElement *helpItem = [[builder menuForIdentifier:UIMenuHelp].children objectAtIndex:0];
UIMenu *helpMenu = [UIMenu menuWithTitle:@"Help " children:[NSArray arrayWithObject:helpItem]];
[builder replaceMenuForIdentifier:UIMenuHelp withMenu:helpMenu];
}
But macOS is not so easily tricked; it still identified this as a help menu and added the search field. Even when I changed the menu name to "Help " as shown here, I got the same result.
Attempt 3: Finally I had to make a new help action, add it to a new help menu, AND name the help menu with an extra space. Only when I did all three things did macOS stop adding the search field:
- (void)buildMenuWithBuilder:(id<UIMenuBuilder>)builder {
[super buildMenuWithBuilder:builder];
UIAction *helpAction = [UIAction actionWithTitle:@"[app name] Help" image:nil identifier:@"simpleHelp" handler:^(__kindof UIAction *action) {
// my help code
}];
UIMenu *helpMenu = [UIMenu menuWithTitle:@"Help " children:[NSArray arrayWithObject:helpAction]];
[builder replaceMenuForIdentifier:UIMenuHelp withMenu:helpMenu];
}