How to set NSView size programmatically?

感情迁移 提交于 2019-11-29 13:41:07

问题


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.


回答1:


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



回答2:


Use the method -setFrameSize: or -setFrame:




回答3:


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


来源:https://stackoverflow.com/questions/4472920/how-to-set-nsview-size-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!