如何以编程方式设置NSView大小?
问题描述:
如何通过编程方式设置NSView的大小,例如
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.
...
(Mac OSX的)笔尖中的大小设置可以正常运行,但是我想用代码来完成.
The size setup in the nib (of Mac OSX) works OK, but I want to do it in code.
答
调用self.frame时,它将返回框架中的数据,而不是指针.因此,结果的任何变化都不会反映在视图中.为了更改视图,您必须在更改后设置新框架:
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;
//...
}