无论方向如何,主屏幕和 self.view 始终保持相同的宽度和高度尺寸(即使 self.view 正确调整大小以适应当前设备方向)这一事实背后的逻辑是什么,而self.view 的 subview 在设备方向改变时返回修改后的宽度和高度尺寸?
NSLog(@"screen bounds w: %f, h: %f", [[UIScreen mainScreen] bounds].size.width,[[UIScreen mainScreen] bounds].size.height );
NSLog(@"self.view frame w: %f, h: %f", self.view.frame.size.width, self.view.frame.size.height );
NSLog(@"myView frame w: %f, h: %f", _myView.frame.size.width, _myView.frame.size.height );
日志输出纵向
2012-11-05 16:15:11.152 test[2807:11303] screen bounds w: 320.000000, h: 480.000000
2012-11-05 16:15:11.153 test[2807:11303] self.view frame w: 320.000000, h: 460.000000
2012-11-05 16:15:11.153 test[2807:11303] myView frame w: 320.000000, h: 460.000000
日志输出景观
2012-11-05 16:14:38.800 test[2807:11303] screen bounds w: 320.000000, h: 480.000000
2012-11-05 16:14:38.801 test[2807:11303] self.view frame w: 300.000000, h: 480.000000
2012-11-05 16:14:38.801 test[2807:11303] myView frame w: 480.000000, h: 300.000000
最佳答案
实际上它不仅仅是自动调整大小的蒙版。因此,当您更改设备的方向时,屏幕的物理特性保持不变。如果您将 View 设置为自动旋转,则会为您翻译宽度和高度。因此,当要求带有翻译 View 的屏幕尺寸时,它的影响。您可以根据 self.view.center
或 self.view.bounds
而不是 self.view.frame
进行计算。
关于objective-c - 为什么 self.view 的高度和宽度与 subview 的高度和宽度不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13228952/