Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

macos - Non-resizable window swift

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

edit/update: Xcode 10.2 ? Swift 5

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:


removing to disable:

window.styleMask.remove(.resizable)

inserting to enable

window.styleMask.insert(.resizable)

Sample

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
        }
    }
}

Sample Project


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...