How to set NSView size programmatically?
Asked Answered
C

3

12

How do you set the size of NSView programmically e.g.

    -(void)awakeFromNib {
        self.frame.size.width   = 1280;   // Does nothing...
        self.frame.size.height  = 800;    // ...neither does this.
        ...

The size setup in the nib (of Mac OSX) works OK, but I want to do it in code.

Crispation answered 17/12, 2010 at 17:3 Comment(0)
O
19

When you call self.frame, it returns the data in the frame, and not a pointer. Therefore, any change in the result is not reflected in the view. In order to change the view, you have to set the new frame after you make changes:

- (void)awakeFromNib {
    NSRect f = self.frame;
    f.size.width = 1280;
    f.size.height = 800;
    self.frame = f;
    //...
}
Orchitis answered 17/12, 2010 at 20:54 Comment(1)
Thanks very much. I had lost the will to live. Now not only has something started happening but I discover that this is the wrong question - the answer to which is below.Crispation
H
8

Use the method -setFrameSize: or -setFrame:

Hotbox answered 17/12, 2010 at 17:27 Comment(1)
Correct answer. Directly changing the frame property seems to not work at least in Swift.Rasia
C
4

To programmatically setup the app's size (that is what I wanted to do) you need to do this:-

- (void)awakeFromNib {
    ...
    NSWindow* w = [self window];
    NSRect f;
    f.size.width  = 1280;
    f.size.height = 800;
    [w setFrame:f display:YES];
}
Crispation answered 18/12, 2010 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.