UIView设置框架怪异

发布于 2024-11-15 20:00:34 字数 973 浏览 2 评论 0原文

我有以下代码:

    - (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2024-11-22 20:00:34

首先(与问题无关,只是样式问题)使用 NSStringFromCGRect 而不是自己打印出来。

NSLog(@"frame = %@", NSStringFromCGRect(frame));
....
if(...) {
    NSLog(@"self.frame = %@", NSStringFromCGRect(self.frame));
}

你的超类是什么视图?它是一个通用的 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.

NSLog(@"frame = %@", NSStringFromCGRect(frame));
....
if(...) {
    NSLog(@"self.frame = %@", NSStringFromCGRect(self.frame));
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文