为什么self.view.frame和self.view.bounds在设备旋转时有所不同?

cha*_*yWu 3 uiviewcontroller uiview ios

我希望在设备旋转时进行一些布局更改.所以我实现了- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration方法来完成工作.但我意识到这个方法被称为self.view.frame和self.view.bounds是不同的.self.view.bounds.size是正确的,self.view.frame.size似乎仍然没有旋转.

例如,我创建了一个空的singleView项目并实现了如下方法:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    NSLog(@"willAnimateRotationToInterfaceOrientation");

    NSLog(@"self.view.bounds.size width:%f height:%f ",self.view.bounds.size.width,self.view.bounds.size.height);
    NSLog(@"self.view.frame.size width:%f height:%f",self.view.frame.size.width,self.view.frame.size.height);

}
Run Code Online (Sandbox Code Playgroud)

当设备从纵向旋转到横向时.输出如下:

2013-06-11 11:57:46.959 viewDemo[95658:707] willAnimateRotationToInterfaceOrientation
2013-06-11 11:57:46.961 viewDemo[95658:707] self.view.bounds.size width:1024.000000 height:748.000000 
2013-06-11 11:57:46.961 viewDemo[95658:707] self.view.frame.size width:748.000000 height:1024.000000
Run Code Online (Sandbox Code Playgroud)

我想知道为什么这些尺寸不同?它们不应该总是一样的吗?什么时候选择使用哪一个?

任何帮助将不胜感激.

Gee*_*eek 5

看看"tc"答案.,截至目前已被接受.将答案中的几行复制到此处.

因此,"框架"相对于父视图,在这种情况下是UIWindow.旋转设备时,窗口不旋转; 视图控制器的视图.从窗口的角度来看,一切都在纵向模式下有效.这就是为什么即使将设备旋转到横向,self.view.frame也不会改变.

您应该使用它self.view.bounds来执行任何计算,因为它会为您提供与设备方向无关的正确值.