You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Objective-C开发的Mac Catalyst应用配置自定义菜单

Objective-C 版 Mac Catalyst 应用移除默认菜单项实现方案

核心实现逻辑

Mac Catalyst 应用的菜单由 UIKit 的 UIMenuBuilder 系统统一管理,你只需要在 AppDelegate 中重写菜单构建方法,即可直接移除或调整默认菜单项。


实现示例

示例1:清空所有默认菜单,仅保留自定义菜单

- (void)application:(UIApplication *)application buildMenuWithBuilder:(id<UIMenuBuilder>)builder {
    // 一键清空所有系统预置菜单
    [builder removeAllMenus];
    
    // 按需添加自定义菜单项,示例添加自定义帮助菜单
    UIAction *helpAction = [UIAction actionWithTitle:@"使用说明" image:nil identifier:nil handler:^(__kindof UIAction * _Nonnull action) {
        // 自定义点击逻辑
        NSLog(@"触发使用说明");
    }];
    UIMenu *customHelpMenu = [UIMenu menuWithTitle:@"帮助" children:@[helpAction]];
    [builder insertChildMenu:customHelpMenu atEndOfMenuForIdentifier:UIMenuRootIdentifier];
}

示例2:针对性移除指定默认菜单,无需全部清空

如果你只需要移除部分默认菜单,不需要全量替换,可以直接根据菜单标识符删除对应项:

- (void)application:(UIApplication *)application buildMenuWithBuilder:(id<UIMenuBuilder>)builder {
    // 移除指定系统菜单
    [builder removeMenuForIdentifier:UIMenuEditIdentifier]; // 移除编辑菜单
    [builder removeMenuForIdentifier:UIMenuFormatIdentifier]; // 移除格式菜单
    [builder removeMenuForIdentifier:UIMenuViewIdentifier]; // 移除视图菜单
    [builder removeMenuForIdentifier:UIMenuWindowIdentifier]; // 移除窗口菜单
    
    // 也可移除某个菜单下的指定子项,示例移除应用菜单下的「偏好设置」
    [builder removeChildrenOfMenuForIdentifier:UIMenuApplicationIdentifier fromChildrenPassingTest:^BOOL(UICommand * _Nonnull child, NSUInteger idx, BOOL * _Nonnull stop) {
        return [child.title isEqualToString:@"偏好设置…"];
    }];
}

注意事项

  • 版本要求:上述 API 仅支持 iOS 14+/macOS 11.0(Big Sur)及以上的 Mac Catalyst 应用
  • 常用系统默认菜单标识符可在 UIMenu.h 头文件中查询,核心包括:
    • UIMenuApplicationIdentifier:最左侧的应用名称菜单
    • UIMenuFileIdentifier:文件菜单
    • UIMenuHelpIdentifier:默认帮助菜单
  • 多场景应用如果需要针对单个场景配置不同菜单,也可以在 SceneDelegate 中单独实现菜单构建逻辑。

内容的提问来源于stack exchange,提问作者coco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 05:06:07