首页 > 解决方案 > 设备倒置时 UIDevice.current.orientation 不一致

问题描述

当我切换UIDevice.current.orientation和打印UIScreen.main.bounds.heightwidth我得到正确的值时,除了设备倒置时。这里heightwidth值是相反的。比如在一个iPhone 8 plus模拟器中,awidth是414, aheight是736。但是当模拟器倒置时,突然awidth是736, aheight是414。代码如下:

switch UIDevice.current.orientation {
    case .landscapeLeft, .landscapeRight:
        print("landscape", " width: ", UIScreen.main.bounds.width, " height: ", UIScreen.main.bounds.height)
    case .portrait:
        print("Portrait", " width: ", UIScreen.main.bounds.width, " height: ", UIScreen.main.bounds.height)
    case .portraitUpsideDown:
        print("UPSIDEDOWN", " width: ", UIScreen.main.bounds.width, " height: ", UIScreen.main.bounds.height)
    default:
        print("other")
}

这给出了以下输出:

landscape  width:  736.0  height:  414.0
UPSIDEDOWN  width:  736.0  height:  414.0
landscape  width:  736.0  height:  414.0
Portrait  width:  414.0  height:  736.0

Portrait 和 PortraitUpsidedown 应该有相同的heightwidth,但是有一个不一致的地方。在纵向模式下,短边应该是,width所以我猜倒置方向有问题。还是我误解了这是如何工作的?

标签: swiftuideviceuideviceorientation

解决方案


推荐阅读