我偶尔想改变一个NSView的维度programmaticaly。要做到这一点,获取各种视图的尺寸,相对于彼此进行调整,然后使用setFrame:来重新定位是很有帮助的。为什么NSView的框架方法返回错误的结果?
问题是,框架方法通常返回明显错误的NSRects,从我的角度来看。
我有一个NSPanel在Interface Builder中建立的程序,以及到我的主程序的各种连接。为了测试框架返回的NSRects,我在应用程序的awakeFromNib:方法中打开了面板,检索了一些NSRects,并将它们打印到控制台。
这是我awakeFromNib:
- (void)awakeFromNib {
NSLog(@"in awakeFromNib");
[self showPrefsSheet:self]; //this is the prefs sheet with the controls of interest
//note that it does indeed pop up and is displayed
originalFrame = [aTextView frame];
NSLog(@"the frame is %d, %d, %d, %d", originalFrame.origin.x, originalFrame.origin.y, originalFrame.size.width, originalFrame.size.height);
originalFrame = [aButton frame];
NSLog(@"the frame is %d, %d, %d, %d", originalFrame.origin.x, originalFrame.origin.y, originalFrame.size.width, originalFrame.size.height);
return;
}
这段代码的结果是这样的控制台:
in awakeFromNib
the frame is 0, 0, 0, 0
the frame is 0, 1079230464, 0, 1078329344
注意,(我)面板与两个控件都显示在屏幕上; (ii)我知道这样一个事实,即网点连接正确,因为我可以通过程序对事件进行控制并让它们工作。 (iii)界面生成器在“尺寸&位置”下的检查器中显示正确的框架尺寸。
简而言之,关于该程序的其他一切都是完美的。它实际上似乎没有设置框架尺寸或其他东西。
有人可以告诉我如何检索真实的帧信息?或者至少解释我看到的结果?
男孩,是我的脸红! 谢谢,凯洛阿。我觉得很愚蠢,但我很高兴能够把它放在我身后。 – Dennis 2009-07-14 19:48:24
没问题。我将添加一个非常有用的宏。 – amattn 2009-07-14 19:51:13