How to constrain second NSViewController minimum size in OS X app?

你。 提交于 2019-12-07 00:43:40

The simplest way to do this would be:

class SecondViewController: NSViewController, NSWindowDelegate {

override func viewWillAppear() {
    super.viewWillAppear()
    self.view.window?.delegate = self
    self.view.window?.minSize = NSSize(width: 100, height: 100)
    }

}

override func viewDidAppear() {
    super.viewDidAppear()
    var frame = self.view.window!.frame
    var initialSize = NSSize(width: 100, height: 100)
    frame.size = initialSize
    self.view.window?.setFrame(frame, display: true)
}

Although if you were looking for a manual approach then the following would work aswell.

class SecondViewController: NSViewController, NSWindowDelegate {

override func viewWillAppear() {
    super.viewWillAppear()
    self.view.window?.delegate = self
    // Set the initial size
    var frame = self.view.window?.frame
    var initialSize = NSSize(width: 100, height: 100)
    frame.size = initialSize
    self.view.window?.setFrame(frame, display: true)
}


func windowWillResize(_ sender: NSWindow, to frameSize: NSSize) -> NSSize {
    let minimumSize = NSSize(width: 100, height: 100)
    var newSize = NSSize()
    if(frameSize.width < minimumSize.width) {
        newSize.width = minimumSize.width
    } else {
        newSize.width = frameSize.width
    }
    if(frameSize.height < minimumSize.height) {
        newSize.height = minimumSize.height
    } else {
        newSize.height = frameSize.height
    }
    return newSize
}


}

Further reading:

Resize windows event

NSWindow minSize

Resizing the window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!