今天同事写代码的时候,发现[UIScreen mainScreen].bounds取出的值和预想的不一样,搞了好长时间,才发现了当前手机使用的放大模式(设置->显示与亮度->放大标准,4.7寸及以上才可以设置),使用了放大模式之后[UIScreen mainScreen].bounds取出的值就有问题了。
下面是在不同机型的不同模式下打印的bounds、nativeBounds、currentMode的值
- IPhone 6Plus 标准
(lldb) po [UIScreen mainScreen].bounds
(origin = (x = 0, y = 0), size = (width = 414, height = 736))
(lldb) po [UIScreen mainScreen].nativeBounds
(origin = (x = 0, y = 0), size = (width = 1080, height = 1920))
(lldb) po [UIScreen mainScreen].currentMode
<UIScreenMode: 0x2823fb7a0; size = 1242.000000 x 2208.000000>
- IPhone 6Plus 放大
(lldb) po [UIScreen mainScreen].bounds
(origin = (x = 0, y = 0), size = (width = 375, height = 667))
(lldb) po [UIScreen mainScreen].nativeBounds
(origin = (x = 0, y = 0), size = (width = 1080, height = 1920))
(lldb) po [UIScreen mainScreen].currentMode
<UIScreenMode: 0x2803d6700; size = 1125.000000 x 2001.000000>
- IPhone 6 标准
(lldb) po [UIScreen mainScreen].bounds
(origin = (x = 0, y = 0), size = (width = 375, height = 667))
(lldb) po [UIScreen mainScreen].nativeBounds
(origin = (x = 0, y = 0), size = (width = 750, height = 1334))
(lldb) po [UIScreen mainScreen].currentMode
<UIScreenMode: 0x109edd6c0; size = 750.000000 x 1334.000000>
- IPhone 6 放大
(lldb) po [UIScreen mainScreen].bounds
(origin = (x = 0, y = 0), size = (width = 320, height = 568))
(lldb) po [UIScreen mainScreen].nativeBounds
(origin = (x = 0, y = 0), size = (width = 750, height = 1334))
(lldb) po [UIScreen mainScreen].currentMode
<UIScreenMode: 0x113b1f6f0; size = 640.000000 x 1136.000000>
- iPhone 5
(lldb) po [UIScreen mainScreen].bounds
(origin = (x = 0, y = 0), size = (width = 320, height = 568))
(lldb) po [UIScreen mainScreen].nativeBounds
(origin = (x = 0, y = 0), size = (width = 640, height = 1136))
(lldb) po [UIScreen mainScreen].currentMode
<UIScreenMode: 0xc57a1b0; size = 640.000000 x 1136.000000>
可以发现
- IPhone6Plus标准的值的bounds是正确的,nativeBounds与IPhone6Plus放大的值是一样的,同时他们的currentMode的值是他们对应的bounds的值 * 3的结果。
- IPhone6标准的值的bounds是正确的,nativeBounds与IPhone6放大的值是一样的,同时他们的currentMode的值是他们对应的bounds的值 * 2的结果。
- IPhone6Plus放大的值的bounds等于IPhone6标准的值,IPhone6放大的值的bounds等于IPhone5的值
结论
在平时开发的过程当中,这些因素可以基本忽略不计,官方文档也是不鼓励去纠结这些,不过也要注意一下,如果你的代码中有通过[UIScreen mainScreen].bounds.size.height 的值去判断当前屏幕尺寸的代码,这是不可取的,用nativeBounds去代替这个值。