【发布时间】:2016-10-20 10:39:42
【问题描述】:
在 macOS 10.12 中,为 NSDocument 应用程序添加了一个新的选项卡栏。您可以阻止工具栏出现(请参阅How do I disable the Show Tab Bar menu option in Sierra apps?)。但是如何去掉添加新窗口的“+”按钮呢?
【问题讨论】:
标签: cocoa tabs nswindow nsdocument macos-sierra
在 macOS 10.12 中,为 NSDocument 应用程序添加了一个新的选项卡栏。您可以阻止工具栏出现(请参阅How do I disable the Show Tab Bar menu option in Sierra apps?)。但是如何去掉添加新窗口的“+”按钮呢?
【问题讨论】:
标签: cocoa tabs nswindow nsdocument macos-sierra
根据 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 中的“新建按钮”部分。
【讨论】:
根据您的应用程序功能,您可以继承 NSDocumentController 并为 documentClassNames 属性返回空数组。
class MyDocumentController: NSDocumentController {
override var documentClassNames: [String] {
return [] // This will disable "+" plus button in NSWindow tab bar.
}
}
这是 documentClassNames 属性的文档:
documentClassNames
表示此应用支持的自定义文档类的字符串数组。数组中的项目是
NSString对象,每个对象代表应用程序支持的文档子类的名称。文档类名称派生自应用程序的Info.plist。您可以覆盖此属性并使用它来返回从插件动态加载的文档类的名称。
这里解释了 documentClassNames 属性如何影响 NSWindow 标签栏和按钮外观:
新按钮
如果在响应者链中实现了
newWindowForTab:,将显示加号按钮。NSDocumentController非正式地实现newWindowForTab:,但如果self.documentClassNames.count > 0并且应用程序具有默认的新文档类型,则仅针对此选择器从respondsToSelector:返回 YES。也就是说,只有NSDocument至少有一个注册的可以编辑的文档类名才会响应。
【讨论】:
改变这个
@IBAction override func newWindowForTab(_ sender: Any?) {}
进入这个
@IBAction func myButton(_ sender: Any?) {}
这将隐藏加号按钮。制表符仍然有效
【讨论】: