【发布时间】:2015-03-30 05:15:33
【问题描述】:
我有一个名为Hardness 的NSViewController,我不需要让用户调整它的大小。当然,每次用户尝试时我都可以将其大小调整回来,但是有什么办法不让用户将窗口打开到全屏或拉伸窗口?
【问题讨论】:
标签: macos swift nswindow window-resize nsviewcontroller
我有一个名为Hardness 的NSViewController,我不需要让用户调整它的大小。当然,每次用户尝试时我都可以将其大小调整回来,但是有什么办法不让用户将窗口打开到全屏或拉伸窗口?
【问题讨论】:
标签: macos swift nswindow window-resize nsviewcontroller
编辑/更新:Xcode 10.2 • Swift 5
NSWindow 有一个名为 styleMask 的属性,它允许您控制哪些类型的控件可供用户使用。如果您不想让用户调整窗口大小,则必须使用变异方法 remove(member: NSWindowStyleMask) 删除样式掩码 .resizable。要再次启用它,您需要使用变异方法insert(member: NSWindowStyleMask)。请注意,它还会禁用该窗口的全屏模式:
移除以禁用:
window.styleMask.remove(.resizable)
插入启用
window.styleMask.insert(.resizable)
样本
import Cocoa
class ViewController: NSViewController {
@IBOutlet weak var closable: NSButton!
@IBOutlet weak var miniaturizable: NSButton!
@IBOutlet weak var resizable: NSButton!
@IBOutlet weak var titled: NSButton!
lazy var window: NSWindow! = self.view.window
func remove(_ member: NSWindow.StyleMask) {
window.styleMask.remove(member)
}
func insert(_ member: NSWindow.StyleMask) {
window.styleMask.insert(member)
}
@IBAction func toggle(_ sender: NSButton) {
switch sender.state {
case .on:
switch sender {
case closable: insert(.closable)
case miniaturizable: insert(.miniaturizable)
case resizable: insert(.resizable)
case closable: insert(.closable)
case titled: insert(.titled)
default: break
}
case .off:
switch sender {
case closable: remove(.closable)
case miniaturizable: remove(.miniaturizable)
case resizable: remove(.resizable)
case closable: remove(.closable)
case titled: remove(.titled)
default: break
}
default: break
}
}
}
【讨论】:
self.view.window?.styleMask = NSClosableWindowMask | NSTitledWindowMask | NSMiniaturizableWindowMask,但没有用
window.styleMask = [.closable, .titled, .miniaturizable] //Swift 3
我通过一行代码解决了不可调整大小窗口的相同问题
override func viewDidAppear() {
self.view.window?.styleMask.remove(NSWindowStyleMask.Resizable)
}
【讨论】:
正确的方法是使用bitwise operators。
禁用调整大小:
window?.styleMask &= ~NSResizableWindowMask
启用调整大小:
window?.styleMask |= NSResizableWindowMask
【讨论】:
Binary operator '|=' cannot be applied to two 'NSWindowStyleMask' operands
window?.styleMask = NSWindowStyleMask(rawValue: (NSWindowStyleMask.closable.rawValue | NSWindowStyleMask.titled.rawValue))
window?.styleMask.update(with: NSWindowStyleMask.resizable)。
除了当前的答案之外,此答案可能会有所帮助。还有一个很好的简单方法可以通过使用setHidden 和NSWindowZoomButton 来完成此操作
将功能设置为NSWindow的子类:
Objective-C
#import "CustomWindow.h"
@implementation CustomWindow
- (void)awakeFromNib {
NSButton *zoomButton = [self standardWindowButton:NSWindowZoomButton];
[zoomButton setHidden:YES];
}
@end
斯威夫特
import CustomWindow
class CustomWindow {
func awakeFromNib() {
var zoomButton: NSButton = self.standardWindowButton(NSWindowZoomButton)
zoomButton.setHidden(true)
}
}
将自定义类连接到您在 IB 中的窗口,现在应该隐藏 Zoom 按钮!
【讨论】:
self.standardWindowButton(NSWindowButton.CloseButton)?.hidden = true ...我认为自从我最初回答了这个问题以来,苹果对其进行了一些更改。
为 Swift 3 提供了一个更优雅的解决方案,这样 |可以使用运算符:
public func | (left: NSWindowStyleMask, right: NSWindowStyleMask) -> NSWindowStyleMask {
return NSWindowStyleMask(rawValue: left.rawValue | right.rawValue)
}
【讨论】: