【问题标题】:Remove (or customize) 'Search' from help menu从帮助菜单中删除(或自定义)“搜索”
【发布时间】:2014-03-31 06:56:54
【问题描述】:

我的应用有默认的“帮助”菜单。我删除了“帮助”条目,并添加了一个链接到我网站上论坛的支持条目。

帮助菜单笔尖如下所示:

但是一旦我启动并运行应用程序,一个新的菜单项就会被吸进去:

如何让搜索消失? (或者更好的是,我怎样才能让它启动一个带有参数的网址,例如http://mywebsite.com/support?search=XXXXX)。

【问题讨论】:

    标签: macos cocoa nsmenu


    【解决方案1】:

    我找到了删除搜索栏的方法(但不是自定义它)。

    只需将一个不使用的菜单分配给帮助菜单即可:

    NSMenu *unusedMenu;
    unusedMenu = [[NSMenu alloc] initWithTitle:@"Unused"];
    
    NSApplication *theApp;
    theApp = [NSApplication sharedApplication];
    theApp.helpMenu = unusedMenu;
    

    文档在 NSApplication 类的 helpMenu 属性中提到了这一点。

    【讨论】:

      【解决方案2】:

      您正在寻找 NSUserInterfaceItemSearching 协议。 返回单个搜索结果项并使用它打开您的自定义 URL。

      - (void)searchForItemsWithSearchString:(NSString *)searchString resultLimit:(NSInteger)resultLimit matchedItemHandler:(void (^)(NSArray *items))handleMatchedItems
      {
          handleMatchedItems(@[searchString]);
      }
      
      - (NSArray *)localizedTitlesForItem:(id)item
      {
          return @[[NSString stringWithFormat:@"Search for '%@' on my website", [item description]]];
      }
      
      - (void)performActionForItem:(id)item
      {
          // Open your custom url assuming item is actually searchString
      }
      

      【讨论】:

      • 对于那些通过 Mac Catalyst 进行 Mac 开发的人:Mac Catalyst 应用程序不支持此功能。
      【解决方案3】:

      您可能不想摆脱那个搜索栏,因为您仍然可以使用它来搜索菜单项!

      我相信您知道,如果您的应用附带 Apple 帮助手册,此搜索框只会显示帮助主题,可以通过关注 Apple's documentation 来制作。

      恐怕我不知道有什么方法可以覆盖搜索栏的行为,但是如果您不想为您的应用编写文档,我认为最好保留搜索栏,即使您无法在论坛中搜索帮助。

      【讨论】:

      • 它只是在 NSApplication 中作为聚光灯帮助或其他东西简要提及。
      • 文档中的当前短语是Spotlight for Help
      【解决方案4】:

      我从 mac 开发的帮助菜单中删除了搜索栏,方法是在帮助后输入一个空格,例如“帮助”。它看起来很有趣,但工作正常。

      【讨论】:

      • 我的应用程序是用 Java 编写的。所以,这个技巧是我可以应用的唯一解决方案。它有效。
      【解决方案5】:

      @zhoudu 的回答对我有用。我把它翻译成 Swift 5.0:

      let unusedMenu = NSMenu(title: "Unused")
      NSApplication.shared.helpMenu = unusedMenu
      

      将其放入您的AppDelegate

      【讨论】:

        【解决方案6】:

        Mac Catalyst 应用程序不支持来自@pointum 和@zhoudu 的答案,所以这里有一个选项。

        根据this answer,搜索栏是macOS添加的。我找了一个plist key 来禁用它,但没有找到任何东西。然后我开始搞乱buildMenuWithBuilder 方法。我必须将AppDelegate 类更改为UIResponder 的子类而不是NSObject,然后我可以在AppDelegate 中覆盖该方法。一旦它运行起来,它需要几次尝试才能用它做一些有用的事情。

        尝试 1:我尝试删除帮助菜单的第一个元素,即搜索字段。

        - (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;
            }];
        }
        

        但是在这个方法运行之后添加了搜索字段,所以这段代码只删除了我想保留的“[app name] Help”元素。

        尝试 2:我尝试从默认菜单中获取“[app name] Help”菜单元素,然后将其添加到新菜单并用该菜单替换默认帮助菜单:

        - (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];
        }
        

        但是 macOS 不是那么容易被骗的;它仍然将其标识为帮助菜单并添加了搜索字段。即使我将菜单名称更改为“帮助”,如此处所示,我得到了相同的结果。

        尝试 3: 最后我必须创建一个新的帮助操作,将它添加到一个新的帮助菜单中,并用一个额外的空格命名帮助菜单。只有当我完成所有三件事时,macOS 才停止添加搜索字段:

        - (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];
        }
        

        【讨论】:

          【解决方案7】:

          @arlomedia 的回答启发,并使用 Swift 5.x、Xcode 11.x,我为appDelegate 创建了一个扩展,该扩展可在 Mac Catalyst 中运行,并完成 OP 想要的两件事:

          1. 创建一个没有Search 项和Support 项的自定义帮助菜单。
          2. 将自定义支持菜单项指向网页。
          import UIKit
          
          extension AppDelegate {
              
              /// selector function to open the desired web page
              @objc func openSupportPage() {
                  if let url = URL(string: "https://mywebsite.com/support?search=XXXXX") {
                      UIApplication.shared.open(url)
                  }
              }
              
              /// buildMenu override to customize the default menu
              override func buildMenu(with builder: UIMenuBuilder) {
                  
                  // ensure we're working with the main menu; and, if so, run `super`!
                  guard builder.system == .main else { return }
                  super.buildMenu(with: builder)
                  
                  // remove the "stock" help menu
                  builder.remove(menu: .help)
                  
                  // create the Support help item; gave it a shortcut key of "s" (Command-S)
                  let supportHelpItem = UIKeyCommand(
                      title: "Support",
                      action: #selector(openSupportPage),
                      input: "s",
                      modifierFlags: [.command])
                  
                  // create a custom menu to appear as *Help* menu on menu bar
                  let myHelpMenu = UIMenu(
                      title: "Help ",                 // note the space after "Help"
                      children: [supportHelpItem])    // add the *Support* item to the menu
                  
                  // insert the *Help* menu at the end of the existing menu bar items
                  // (after the Window item) where the Help menu would've appeared
                  builder.insertSibling(myHelpMenu, afterMenu: .window)
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-02-13
            • 2018-02-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多