Den*_*nis 3 formatting cocoa nslog nsview
我偶尔想要改变一个NSView的维度.为此,获取各种视图的尺寸,相对于彼此调整它们,然后使用setFrame:重新定位是有帮助的.
问题是,从我的角度来看,frame方法通常会返回明显错误的NSRects.
我有一个程序,在Interface Builder中设置了NSPanel,并与我的主程序有各种连接.为了测试框架返回的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;
}
Run Code Online (Sandbox Code Playgroud)
此代码的结果在控制台中如下所示:
in awakeFromNib
the frame is 0, 0, 0, 0
the frame is 0, 1079230464, 0, 1078329344
Run Code Online (Sandbox Code Playgroud)
请注意,(i)面板和两个控件都显示在屏幕上; (ii)我知道这些插座是正确连接的,因为我可以通过编程方式对控件进行操作并让它们工作; (iii)界面构建器在"尺寸和位置"下的检查器中显示正确的框架尺寸.
简而言之,该计划的其他一切工作正常.实际上似乎框架尺寸没有正确设置或其他东西.
有人能告诉我如何检索真实的帧信息吗?或者至少解释我看到的结果?
帧坐标是浮点数,但您的日志消息使用%d.(整数).
将NSLog字符串更改为:
@"the frame is %f, %f, %f, %f"
Run Code Online (Sandbox Code Playgroud)
编辑:
这是调试rects时使用的一个非常有用的宏:
#define RECTLOG(rect) (NSLog(@"" #rect @" x:%f y:%f w:%f h:%f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height ));
Run Code Online (Sandbox Code Playgroud)
然后,您可以执行简单的rect日志:
RECTLOG([aTextView frame]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1582 次 |
| 最近记录: |