self.view.frame在横向模式下意外更改大小 [英] self.view.frame changed size ifself unexpectedly in landscape mode

查看:113
本文介绍了self.view.frame在横向模式下意外更改大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过IB设置了视图控制器的视图.该视图设置为宽480,高320的风景.它已正确加载到模拟器中.但是,当我使用NSLog检查self.frame.size时,我发现帧大小发生了变化,而无需旋转设备或任何代码.这是我得到的:

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:

在viewDidLoad中->尺寸= 480 x 320

In viewDidLoad --> size = 480 x 320

在viewWillAppear中->尺寸= 480 x 320

In viewWillAppear --> size = 480 x 320

在viewDidAppear中->尺寸= 320 x 480

In viewDidAppear --> size = 320 x 480

我在viewDidLoad中只有代码,而在viewWillAppear和viewDidAppear中没有代码.有人知道幕后发生了什么吗?谢谢.

I only have codes in viewDidLoad, but not viewWillAppear nor viewDidAppear. Anyone knows what happened behind the scene? Thanks.

推荐答案

尝试如下操作:

NSLog("frame=%@ transform=%@", NSStringFromCGRect(self.frame), NSStringFromCGAffineTransform(self.transform));

我敢打赌,当帧大小为480x320且帧数为90°时,变换就是标识(1,0,0,1,0,0).当帧尺寸为320x480时旋转(0,1,-1,0,0,0或0,-1,1,0,0,0).

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.

这是怎么回事?

好吧,在将视图添加到视图层次结构之前,将调用viewDidLoad(和我 think viewWillAppear).将UIKit添加到层次结构后,它还会设置视图转换,即旋转的发生方式.

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?

好吧,框架"是相对于父视图(即UIWindow)的.窗户不转动.视图控制器的视图可以.从窗口的角度来看,所有内容都有效地处于纵向模式.

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.

您可能想要self.view.bounds.

You probably want self.view.bounds.

这篇关于self.view.frame在横向模式下意外更改大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆