简体   繁体   中英

iPhone 6+ screen dimension confusion

When printing the screen height of the iPhone 6+ simulator I get 2208, as expected. However when I test on an actual iPhone 6+ I get 1920. Very confused about which one is right. Here is the code I'm using:

func test() {
    if UIDevice().userInterfaceIdiom == .phone {
        switch UIScreen.main.nativeBounds.height {
        case 1136:
            print("iPhone 5 or 5S or 5C")
        case 1334:
            print("iPhone 6/6S/7/8")
        case 2208:
            print("iPhone 6+/6S+/7+/8+")
        case 2436:
            print("iPhone X")
        default:
            print("unknown")
        }
    }
}

问题是所有其他设备都对其进行了下采样,因此要解决此问题,您需要测试两种不同的分辨率,一种用于模拟器2208,另一种用于真实设备1920,您可以在此答案中看到:

case 1920, 2208:

Videos, OpenGL, and other things based on CALayers that deal with pixels will deal with the real 1920x1080 frame buffer on device (or 2208x1242 on sim). Things dealing with points in UIKit will be deal with the 2208x1242 (x3) bounds and get scaled as appropriate on device.

The simulator does not have access to the same hardware that is doing the scaling on device and there's not really much of a benefit to simulating it in software as they'd produce different results than the hardware. Thus it makes sense to set the nativeBounds of a simulated device's main screen to the bounds of the physical device's main screen.

iOS 8 added API to UIScreen ( nativeScale and nativeBounds ) to let a developer determine the resolution of the CADisplay corresponding to the UIScreen .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM