I occasionally want to change an NSView's dimensions programmaticaly. To do this, it is helpful to get the dimensions of various views, adjust them relative to each other, and then use setFrame: to reposition.
我偶尔想要改变一个NSView的维度。为此,获取各种视图的尺寸,相对于彼此调整它们,然后使用setFrame:重新定位是有帮助的。
The problem is that the frame method usually returns NSRects that are clearly wrong, from my perspective.
问题是,从我的角度来看,frame方法通常会返回明显错误的NSRects。
I have a program with an NSPanel set up in Interface Builder and various connections to my main program. To test the NSRects returned by frame, I opened the panel in my application's awakeFromNib: method, retrieved some NSRects, and printed them to the console.
我有一个程序,在Interface Builder中设置了NSPanel,并与我的主程序有各种连接。为了测试框架返回的NSRects,我在应用程序的awakeFromNib:方法中打开了面板,检索了一些NSRects,并将它们打印到控制台。
Here is my awakeFromNib:
这是我的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;
}
The results of this code look like this in the console:
此代码的结果在控制台中如下所示:
in awakeFromNib
the frame is 0, 0, 0, 0
the frame is 0, 1079230464, 0, 1078329344
Note that (i) the panel and both controls are displayed on the screen; (ii) I know for a fact that the outlets are properly connected, because I can do things to the controls programmatically and have them work; (iii) interface builder shows correct frame sizes in the Inspector under "Size & Position".
请注意,(i)面板和两个控件都显示在屏幕上; (ii)我知道这些插座是正确连接的,因为我可以通过编程方式对控件进行操作并让它们工作; (iii)界面构建器在“尺寸和位置”下的检查器中显示正确的框架尺寸。
In short, everything else about the program is working perfectly. It actually seems that the frame dimensions aren't set properly or something.
简而言之,该计划的其他一切工作正常。实际上似乎框架尺寸没有正确设置或其他东西。
Can somebody tell me how to retrieve the real frame information? Or at least explain the results I am seeing?
有人能告诉我如何检索真实的帧信息吗?或者至少解释我看到的结果?
2 个解决方案
#1
The frame coordinates are floats, but your log messages are using %d. (ints).
帧坐标是浮点数,但您的日志消息使用%d。 (整数)。
change the NSLog string to:
将NSLog字符串更改为:
@"the frame is %f, %f, %f, %f"
Edited:
Here's a very useful macro I use when debugging rects:
这是调试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 ));
You can then do easy rect logs:
然后,您可以执行简单的rect日志:
RECTLOG([aTextView frame]);
#2
When logging an NSRect, remember that the fields are floating point values, not integers.
记录NSRect时,请记住字段是浮点值,而不是整数。
Rather than writing the format specifiers yourself (either inline or via a macro), use the functions that the framework provides for you:
而不是自己编写格式说明符(内联或通过宏),使用框架为您提供的函数:
NSLog(@"frame - %@", NSStringFromRect([view frame]));
See also:
NSStringFromSize
NSStringFromPoint
NSStringFromRange
etc
#1
The frame coordinates are floats, but your log messages are using %d. (ints).
帧坐标是浮点数,但您的日志消息使用%d。 (整数)。
change the NSLog string to:
将NSLog字符串更改为:
@"the frame is %f, %f, %f, %f"
Edited:
Here's a very useful macro I use when debugging rects:
这是调试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 ));
You can then do easy rect logs:
然后,您可以执行简单的rect日志:
RECTLOG([aTextView frame]);
#2
When logging an NSRect, remember that the fields are floating point values, not integers.
记录NSRect时,请记住字段是浮点值,而不是整数。
Rather than writing the format specifiers yourself (either inline or via a macro), use the functions that the framework provides for you:
而不是自己编写格式说明符(内联或通过宏),使用框架为您提供的函数:
NSLog(@"frame - %@", NSStringFromRect([view frame]));
See also:
NSStringFromSize
NSStringFromPoint
NSStringFromRange
etc