if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
        print("landscape")
    }
    if UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation){
        print("portrait")
    }

我怎样才能判断它是左还是右?

推荐答案

你可以这样做,

if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft{

}
else if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight{

}
else if UIDevice.currentDevice().orientation == UIDeviceOrientation.UIDeviceOrientationPortraitUpsideDown{

}
else if UIDevice.currentDevice().orientation == UIDeviceOrientation.UIDeviceOrientationPortrait{

}

Swift 5

    if UIDevice.current.orientation.isLandscape {

    } else if UIDevice.current.orientation.isFlat {

    } else if UIDevice.current.orientation.isPortrait {

    } else if UIDevice.current.orientation.isValidInterfaceOrientation {

    }

Swift 3

if UIDevice.current.orientation == UIDeviceOrientation.landscapeLeft {

} else if UIDevice.current.orientation == UIDeviceOrientation.landscapeRight {

} else if UIDevice.current.orientation == UIDeviceOrientation.portrait {

} else if UIDevice.current.orientation == UIDeviceOrientation.portraitUpsideDown {

        } 

Swift相关问答推荐

在运行时检测蒸汽工人的类型

在visionOS 1.0中已弃用';base Color';:请改用`Color`属性

使用序列初始化字符串的时间复杂度是多少?

为什么 Swift URL 的 init?(string: String,relativeTo: URL?) 仅添加协议?

BLE 在 ESP32 和 Iphone 之间发送字符串

使用索引来访问 libc 定义的元组

为什么 String.contains 在我导入 Foundation 时表现不同?

如何测试可选 struct 的协议一致性

动画偏移正在 destruct 按钮 SwiftUI

如何使用 ISO 国家代码(2 个字母)查找区域设置标识符

如何有条件地依赖桌面与 iOS 的系统库?

不要从 NumberFormatter 获取货币符号

如何在 SwiftUI 的 fileImporter 中为 allowedContentTypes 设置 xcodeproj 类型?

无法使用锚激活约束

如何在 SwiftUI 中实现 PageView?

NSFontAttributeName 已更改为 String

使用 Swift 在 iOS 8 中为应用程序图标添加徽章

Swift - 导入我的 swift 类

如何在 SwiftUI 中以编程方式滚动列表?

在 unwind segue 之后执行 push segue