UIView设置框架怪异
我有以下代码:
- (id)initWithFrame:(CGRect)frame chapterNum:(int)newChapterNumber pageNum:(int)newPageNumber
{
NSLog (@"Frame:x:%f y:%f width:%f height:%f",frame.origin.x,
frame.origin.y,
frame.size.width,
frame.size.height);
self = [super initWithFrame:frame];
if (self) {
NSLog (@"View Frame:x:%f y:%f width:%f height:%f",self.frame.origin.x,
self.frame.origin.y,
self.frame.size.width,
self.frame.size.height);
......
}
我在模拟器上运行(横向模式),当我查看控制台日志的结果时,我看到:
2011-06-17 09:17:16.428 MyApp[16454:207] Frame:x:0.000000 y:0.000000 宽度:128.000000 高度:96.000000
2011-06-17 09:17:16.433 MyApp[16454:207] View Frame:x:-64.000000 y:-48.000000 width:128.000000 height:96.000000
为什么 View 框架的原点与我使用的框架不同,值得注意的是,该值恰好是大小变量(加一个 - 号)。非常感谢任何帮助!
I have the following code:
- (id)initWithFrame:(CGRect)frame chapterNum:(int)newChapterNumber pageNum:(int)newPageNumber
{
NSLog (@"Frame:x:%f y:%f width:%f height:%f",frame.origin.x,
frame.origin.y,
frame.size.width,
frame.size.height);
self = [super initWithFrame:frame];
if (self) {
NSLog (@"View Frame:x:%f y:%f width:%f height:%f",self.frame.origin.x,
self.frame.origin.y,
self.frame.size.width,
self.frame.size.height);
......
}
Im running on the simulator (in landscape mode) when I look at the results of the console logs i see:
2011-06-17 09:17:16.428 MyApp[16454:207] Frame:x:0.000000 y:0.000000 width:128.000000 height:96.000000
2011-06-17 09:17:16.433 MyApp[16454:207] View Frame:x:-64.000000 y:-48.000000 width:128.000000 height:96.000000
Why is the origin of the View frame not the same as the frame im using , is it noteworthy that the value are exactly half of the size variables (plus a - sign). Any help is greatly appreciated !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先(与问题无关,只是样式问题)使用 NSStringFromCGRect 而不是自己打印出来。
你的超类是什么视图?它是一个通用的 UIView 还是你自己制作的?您的
initWithFrame:
方法中的 .... 之前没有任何内容吗?宽度和高度的一半成为新原点的意义在于,您的视图已将其
center
移动到0,0。不知道为什么会这样做,因此是关于你的超类的问题。有关更多信息,请参阅此处的文档 并查看
center
属性的文档。First (and has nothing to do with the problem, just a style issue) use NSStringFromCGRect instead of printing it out yourself.
What view is your superclass? Is it a generic UIView or one of your making? Is there nothing in your
initWithFrame:
method before the ....?The significance of half your width and height becoming the new origin is that your view has had it's
center
moved to 0,0. Not sure why it would be doing that, thus the question about your superclass.For more info see the docs here and look at the docs for the
center
property.