self.view.frame 在横向模式下意外更改了大小
我已经通过 IB 设置了视图控制器的视图。视图设置为横向,宽度为 480,高度为 320。它在模拟器中正确加载。然而,当我使用 NSLog 检查 self.frame.size 时,我发现帧大小会自行更改,而无需任何设备旋转或任何代码。这是我得到的:
In viewDidLoad -->尺寸 = 480 x 320
在 viewWillAppear -->大小= 480 x 320
在viewDidAppear --> size = 320 x 480
我只有viewDidLoad中的代码,但没有viewWillAppear或viewDidAppear。有谁知道幕后发生了什么?谢谢。
I've setup the view controller's view via IB. The view is set to landscape with width 480 and height 320. It loaded correctly in the simulator. However, when I checked self.frame.size by using NSLog, I found that the frame size changed itself without any rotation of device or any code. Here is what I get:
In viewDidLoad --> size = 480 x 320
In viewWillAppear --> size = 480 x 320
In viewDidAppear --> size = 320 x 480
I only have codes in viewDidLoad, but not viewWillAppear nor viewDidAppear. Anyone knows what happened behind the scene? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样的事情:
我愿意打赌,当帧大小为 480x320 时,变换是恒等式 (1,0,0,1,0,0),并且 90° 旋转 (0,1,-1,0,当帧大小为 320x480 时,为 0,0 或 0,-1,1,0,0,0)。
这是怎么回事?
好吧,viewDidLoad(我认为 viewWillAppear)是在视图添加到视图层次结构之前调用的。当它添加到层次结构时,UIKit 还会设置视图变换,这就是旋转发生的方式。
但为什么这会改变框架呢?
好吧,“框架”是相对于父视图(即 UIWindow)而言的。窗户不旋转;视图控制器的视图确实如此。从窗口的角度来看,一切都有效地处于纵向模式。
您可能需要 self.view.bounds。
Try something like this:
I'm willing to bet that transform is the identity (1,0,0,1,0,0) when frame size is 480x320, and a 90° rotation (0,1,-1,0,0,0 or 0,-1,1,0,0,0) when frame size is 320x480.
What's going on?
Well, viewDidLoad (and I think viewWillAppear) are called before the view is added to the view hierarchy. When it's added to the hierarchy, UIKit also sets the view transform, which is how rotation happens.
But why does that change the frame?
Well, the "frame" is relative to the parent view, which is the UIWindow. The window doesn't rotate; the view controller's view does. From the window's perspective, everything is effectively in portrait mode.
You probably want self.view.bounds.