Search code examples
macoscocoaswiftresizewindow

Resizing the window according to a variable swift


I have a NSViewController and a variable num. I want to change the size of the window dynamically according to that variable. Is there any way to do that in swift?


Solution

  • Let's say your window has an IBOutlet named "window", and your dynamic number is named "myDynamicNumber":

    func resize() {
        var windowFrame = window.frame
        let oldWidth = windowFrame.size.width
        let oldHeight = windowFrame.size.height
        let toAdd = CGFloat(myDynamicNumber)
        let newWidth = oldWidth + toAdd
        let newHeight = oldHeight + toAdd
        windowFrame.size = NSMakeSize(newWidth, newHeight)
        window.setFrame(windowFrame, display: true)
    }