XYZ
XYZ

Reputation: 27387

How to change window frame size without change its location

self.view.window?.frame.size.width = 430
self.view.window?.frame.size.height = 310

The height and width are read-only, so seems the only way to change the dimension of a frame is to re-assign an NSRect to it using code below,

self.view.window?.setFrame(NSMakeRect(0, 0, 430, 310), display: true, animate: true)

Is there a way to change window frame size without change its location?

Upvotes: 1

Views: 1258

Answers (2)

Marcy
Marcy

Reputation: 6009

As Gerd K said, the y coordinate needs to be adjusted. To save others time, here is that calculation fashioned after pbodsk's example:

func resizeFrame(newWidth: CGFloat, newHeight: CGFloat) {

    if let originalFrame = view.window?.frame {
        let newY = originalFrame.origin.y + originalFrame.size.height - newHeight
        view.window?.setFrame(NSRect(x: originalFrame.origin.x, y: newY, width: newWidth, height: newHeight), display: true, animate: true)
    }
}

Usage:

resizeFrame(newWidth: 430, newHeight: 310)

Upvotes: 2

pbodsk
pbodsk

Reputation: 6876

You can use the origin of the original frame and make a new NSRect with a different size.

Something like this for instance:

func resizeFrame(newWidth: CGFloat, newHeight: CGFloat) {
    if let originalFrame = view.window?.frame {
        let newSize = CGSize(width: newWidth, height: newHeight)
        view.window?.setFrame(NSRect(origin: originalFrame.origin, size: newSize), display: true, animate: true)
    }
}

Used like this:

resizeFrame(newWidth: 430, newHeight: 310)

Hope that helps.

Upvotes: 2

Related Questions