隐藏 NSWindow 新标签按钮

Hide NSWindow New tab button

在 macOS 10.12 中,NSWindows 为 NSDocument 应用程序添加了一个新的标签栏。您可以阻止工具栏出现(请参阅 )。但是如何删除添加新的“+”按钮Windows?

根据 AppKit 发行说明,在 NSDocumentController 子类中返回 false 以响应 newWindowForTab(_:) 操作消息会禁用选项卡栏中的“+”按钮。

override func responds(to aSelector: Selector!) -> Bool {

    if #available(OSX 10.12, *) {
        if aSelector == #selector(NSResponder.newWindowForTab(_:)) {
            return false
        }
    }

    return super.responds(to: aSelector)
}

请参阅 AppKit Release Notes for macOS 10.12 中的“新按钮”部分。

只需在您的 NSWindow 的 Interface Builder 中将“Tabbing Mode”设置为 Disallowed。

改变这个

@IBAction override func newWindowForTab(_ sender: Any?) {}

进入这个

@IBAction func myButton(_ sender: Any?) {}

这将隐藏加号按钮。标签仍然有效

根据您的应用程序功能,您可以将 NSDocumentController 和 return 空数组子类化为 documentClassNames 属性。

class MyDocumentController: NSDocumentController {

   override var documentClassNames: [String] {
      return [] // This will disable "+" plus button in NSWindow tab bar.
   }
}

这是 documentClassNames 属性:

的文档

documentClassNames
An array of strings representing the custom document classes supported by this app.

The items in the array are NSString objects, each of which represents the name of a document subclasses supported by the app. The document class names are derived from the app’s Info.plist. You can override this property and use it to return the names of document classes that are dynamically loaded from plugins.

Source

下面是 documentClassNames 属性 如何影响 NSWindow 标签栏加按钮外观的解释:

New Button

The plus button will be shown if newWindowForTab: is implemented in the responder chain. NSDocumentController informally implements newWindowForTab:, but only returns YES from respondsToSelector: for this selector if the self.documentClassNames.count > 0 and if the app has a default new document type. In other words, it only responds to it if NSDocument has at least one registered document class name which can be edited.

Source