How can I set the window size programmatically? I have a window in IB and I want to set the size of it in my code to make it larger.
22
votes
6 Answers
33
votes
Use -setFrame:display:animate:
for maximum control:
NSRect frame = [window frame];
frame.size = theSizeYouWant;
[window setFrame: frame display: YES animate: whetherYouWantAnimation];
Note that window coordinates are flipped from what you might be used to. The origin point of a rectangle is at its bottom left in Quartz/Cocoa on OS X. To ensure the origin point remains the same:
NSRect frame = [window frame];
frame.origin.y -= frame.size.height; // remove the old height
frame.origin.y += theSizeYouWant.height; // add the new height
frame.size = theSizeYouWant;
// continue as before
12
votes
7
votes
4
votes
0
votes
my two cents for swift 4.x 7 OSX:
a) do not call on viewDidLoad b) go on main queue... b) wait some time... so for example use:
private final func setSize(){
if let w = self.view.window{
var frame = w.frame
frame.size = NSSize(width: 400, height: 800)
w.setFrame(frame, display: true, animate: true)
}
}