不可调整大小的窗口快速 [英] Non-resizable window swift
问题描述
我有一个名为Hardness
的NSViewController
,并且不需要让用户调整其大小.当然,我可以在每次用户尝试时重新调整它的大小,但是有什么办法不让用户将窗口打开到全屏或拉伸窗口?
I have a NSViewController
named Hardness
, and I need not to let user resize it. Of course, I can just resize it back every time the users tries, but is there any way just not to let user open a window to full screen, or to stretch the window?
推荐答案
编辑/更新: Xcode 10.2•Swift 5
NSWindow具有一个称为styleMask的属性,该属性使您可以控制用户可以使用哪种控件.如果不想让用户调整窗口大小,则必须使用变异方法remove(member: NSWindowStyleMask)
删除样式掩码.resizable.要再次启用它,您需要使用变异方法insert(member: NSWindowStyleMask)
.请注意,它还将禁用该窗口的全屏模式:
NSWindow has a property called styleMask that allows you to control what kinds of control will be available to the user. If you don't want to allow the user to resize the window you have to remove the style mask .resizable using the mutating method remove(member: NSWindowStyleMask)
. To enable it again you need to use the mutating method insert(member: NSWindowStyleMask)
. Note that it will also disable the full screen mode for that window:
删除以禁用:
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
}
}
}
这篇关于不可调整大小的窗口快速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!