深入了解iOS开发中UIWindow的相关使用

2020-01-14 16:06:00丽君

window bounds: {{0, 0}, {320, 480}}

2014-06-04 09:26:34.788 SviOS8[4143:61114] system ver: 8.0,

orientaion: UIInterfaceOrientationPortrait,

window bounds: {{0, 0}, {480, 320}}

2014-06-04 09:26:35.791 SviOS8[4143:61114] system ver: 8.0,

orientaion: UIInterfaceOrientationLandscapeLeft,

window bounds: {{0, 0}, {320, 480}}

2014-06-04 09:26:47.468 SviOS8[4143:61114] system ver: 8.0,

orientaion: UIInterfaceOrientationPortraitUpsideDown,

window bounds: {{0, 0}, {480, 320}}

 


  iOS 7及之前的版本运行结果为:
 

 

复制代码 2014-06-04 09:39:00.527 SviOS8[4380:70b] system ver: 7.0.3,

 

orientaion: UIInterfaceOrientationLandscapeRight,

window bounds: {{0, 0}, {320, 480}}

2014-06-04 09:39:00.895 SviOS8[4380:70b] system ver: 7.0.3,

orientaion: UIInterfaceOrientationPortrait,

window bounds: {{0, 0}, {320, 480}}

2014-06-04 09:39:01.225 SviOS8[4380:70b] system ver: 7.0.3,

orientaion: UIInterfaceOrientationLandscapeLeft,

window bounds: {{0, 0}, {320, 480}}

2014-06-04 09:39:11.004 SviOS8[4380:70b] system ver: 7.0.3,

orientaion: UIInterfaceOrientationPortraitUpsideDown,

window bounds: {{0, 0}, {320, 480}}

 


  通过对比我们可以清晰的看到iOS 8中UIWindow在处理旋转时策略的变更,虽然会因为与之前的版本不同导致现有项目布局存在的bug,但是可以看到iOS 8中的处理方式更加符合我们的预期,在竖向的时候我们就获取到width < height, 在横向则是 width > height,这个符合所见即所得的原则。
 
  题外话,不管是iOS 7还是之前的版本,以及最新出的iOS 8,所有的ViewController的bounds都是正确的,所以只需要坚持一个原则“所有布局都是基于VC.view.bounds布局,那么你的App的显示就一切正常。”