macos 如何以编程方式设置 NSView 大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4472920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set NSView size programmatically?
提问by Robin Pain
How do you set the size of NSView programmically e.g.
你如何以编程方式设置 NSView 的大小,例如
-(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.
(Mac OSX 的)笔尖中的大小设置工作正常,但我想在代码中进行设置。
回答by ughoavgfhw
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:
当您调用 self.frame 时,它返回帧中的数据,而不是指针。因此,结果中的任何更改都不会反映在视图中。为了更改视图,您必须在进行更改后设置新框架:
- (void)awakeFromNib {
NSRect f = self.frame;
f.size.width = 1280;
f.size.height = 800;
self.frame = f;
//...
}
回答by ericg
Use the method -setFrameSize: or -setFrame:
使用方法 -setFrameSize: 或 -setFrame:
回答by Robin Pain
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];
}