我使用以下代码查看设备是否处于横向模式:
UIDevice.currentDevice().orientation.isLandscape.boolValue
如果我在启动应用程序之前将我的设备置于横向模式,并且在 viewDidLoad
之后,我调用这行代码,它总是会返回 false
,但它可以工作。
如果我改用它:
interfaceOrientation.isLandscape
它返回 true
,这是正确的,但编译器显示警告,指出 interfaceOrientation 在 iOS 8.0 中已弃用
。
应用启动后立即获取设备方向的正确方法是什么?
最佳答案
DeviceOrientation vs. ScreenSize vs StatusBar.isLandscape?
iOS 11、Swift 4 和 Xcode 9.X
无论是否使用 AutoLayout,有几种方法可以获得正确的设备方向,它们可用于检测使用应用程序时的旋转变化,以及在应用程序启动时或从后台恢复后获得正确的方向.
此解决方案在 iOS 11 和 Xcode 9.X 中运行良好
<强>1。 UIScreen.main.bounds.size:
如果您只想知道应用程序是横向模式还是纵向模式,最好的起点是启动时 rootViewController
中的 viewDidLoad
和 viewWillTransition (toSize:)
在 rootViewController
中,如果您想在应用程序处于后台时检测旋转变化,并且应该以正确的方向恢复 UI。
let size = UIScreen.main.bounds.size
if size.width < size.height {
print("Portrait: \(size.width) X \(size.height)")
} else {
print("Landscape: \(size.width) X \(size.height)")
}
这也发生在 app/viewController 生命周期的早期。
<强>2。通知中心
如果需要获取实际的设备方向(包括faceDown、faceUp等)。你想按如下方式添加一个观察者(即使你在 AppDelegate
的 application:didFinishLaunchingWithOptions
方法中这样做,第一个通知也可能在 之后触发viewDidLoad
被执行
device = UIDevice.current
device?.beginGeneratingDeviceOrientationNotifications()
notificationCenter = NotificationCenter.default
notificationCenter?.addObserver(self, selector: #selector(deviceOrientationChanged),
name: Notification.Name("UIDeviceOrientationDidChangeNotification"),
object: nil)
并按如下方式添加选择器。我将它分成两部分,以便能够在 viewWillTransition
inspectDeviceOrientation()
@objc func deviceOrientationChanged() {
print("Orientation changed")
inspectDeviceOrientation()
}
func inspectDeviceOrientation() {
let orientation = UIDevice.current.orientation
switch UIDevice.current.orientation {
case .portrait:
print("portrait")
case .landscapeLeft:
print("landscapeLeft")
case .landscapeRight:
print("landscapeRight")
case .portraitUpsideDown:
print("portraitUpsideDown")
case .faceUp:
print("faceUp")
case .faceDown:
print("faceDown")
default: // .unknown
print("unknown")
}
if orientation.isPortrait { print("isPortrait") }
if orientation.isLandscape { print("isLandscape") }
if orientation.isFlat { print("isFlat") }
}
请注意,UIDeviceOrientationDidChangeNotification
可能会在启动期间发布多次,在某些情况下,它可能是 .unknown
。我所看到的是,第一个正确的方向通知是在 viewDidLoad
和 viewWillAppear
方法之后,在 viewDidAppear
之前,甚至是 applicationDidBecomeActive
方向对象将为您提供所有 7 种可能的场景(来自 enum UIDeviceOrientation
定义):
public enum UIDeviceOrientation : Int {
case unknown
case portrait // Device oriented vertically, home button on the bottom
case portraitUpsideDown // Device oriented vertically, home button on the top
case landscapeLeft // Device oriented horizontally, home button on the right
case landscapeRight // Device oriented horizontally, home button on the left
case faceUp // Device oriented flat, face up
case faceDown // Device oriented flat, face down
}
有趣的是,isPortrait
只读 Bool
变量在 UIDeviceOrientation
的扩展中定义如下:
extension UIDeviceOrientation {
public var isLandscape: Bool { get }
public var isPortrait: Bool { get }
public var isFlat: Bool { get }
public var isValidInterfaceOrientation: Bool { get }
}
<强>3。 StatusBarOrientation
UIApplication.shared.statusBarOrientation.isLandscape
这也可以很好地确定方向是纵向还是横向,并给出与第 1 点相同的结果。您可以在 viewDidLoad
(用于应用程序启动)和 viewWillTransition( toSize:)
如果来自背景。但它不会为您提供通过通知获得的顶部/底部、左/右、上/下的详细信息(第 2 点)
关于ios - 在 Swift 中,如何在启动后立即正确获取设备方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34452650/