开发者

How to set NSView size programmatically?

开发者 https://www.devze.com 2023-01-31 16:40 出处:网络
How do you set the size of NSView programmically e.g. -(void)awakeFromNib { 开发者_StackOverflow self.frame.size.width= 1280;// Does nothing...

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

    -(void)awakeFromNib {
       开发者_StackOverflow 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.


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;
    //...
}


Use the method -setFrameSize: or -setFrame:


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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消