美文网首页
NSToolbar 只保留自定义工具栏选项

NSToolbar 只保留自定义工具栏选项

作者: 高思阳 | 来源:发表于2018-10-18 11:20 被阅读45次

    Updated 1:

    According to @Khundragpan and [this post](https://stackoverflow.com/questions/8413111/how-to-customize-the-context-menu-of-nstoolbar), problem 1 can be solved by:
    
     if let contextMenu = window?.contentView?.superview?.menu {
    
      for item in contextMenu.items {
    
        if item.title != "Customize Toolbar…" {
          contextMenu.removeItem(item)
        }
    
      }
    
    }
    

    But I don't think it's the best way.

    Update 2:

    Another way to solve problem 1 (thanks to @1024jp to point out this file):

     if let contextMenu = window?.contentView?.superview?.menu {
    
       contextMenu.items.forEach({ (item) in
    
        if let action = item.action,
    
        NSStringFromSelector(action) != "runToolbarCustomizationPalette:" {
    
          contextMenu.removeItem(item)
    
        }
    
      })
    
    }
    

    Update 3:

    A ton of thanks to @1024jp for helping me. I'm able to remove those things with a few tips and tricks from him. Check the answer below.

    You can access and modify a toolbar contextual menu when the toolbar is created, i.e. in -[awakeFromNib]:

     - (NSMenu *)toolbarMenuInWindow:(NSWindow *)window{
    
      NSView *contentView = window.contentView;
    
      NSView *toolbarView = contentView.superview.subviews.lastObject;
    
      NSMenu *toolbarMenu = toolbarView.menu;
    
      return toolbarMenu;
    
    } 
    

    Now you can directly edit menu items and hide or disable them.

    相关文章

      网友评论

          本文标题:NSToolbar 只保留自定义工具栏选项

          本文链接:https://www.haomeiwen.com/subject/wwujzftx.html