'statusBarOrientation' was deprecated in iOS 13.0 when attempting to get app orientation
Simple put, I was relying on the following code to provide the orientation of the application. This is utilized for several reasons within the application:
- Per UX specification, the layout of the stackview is set based upon orientation for iPad (horizontal when in landscape, vertical when in portrait)
- Building on the previous item, the stackview is placed on the screen to the left (portrait) or on the top (landscape)
- There is custom rotation logic that responds differently based on the status. tl;dr is that on an iPad, the app presents itself with significant differences between orientation
I am just tasked with maintenance in this scenario and do not have the ability to make significant changes that deviate from the current (and properly functioning) layout logic in place.
As of now, it relies upon the following to capture application orientation:
var isLandscape: Bool {
return UIApplication.shared.statusBarOrientation.isLandscape
}
However, with the Xcode 11 GM version, I am given the following deprecation warning:
'statusBarOrientation' was deprecated in iOS 13.0: Use the interfaceOrientation property of the window scene instead.
How can I go about getting the orientation of the application via status bar?
Swift 5, iPadOS 13, taking the multi-window environment into account:
if let interfaceOrientation = UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.windowScene?.interfaceOrientation {
// Use interfaceOrientation
}
Swift 5, iOS 13, but compatible with older versions of iOS:
extension UIWindow {
static var isLandscape: Bool {
if #available(iOS 13.0, *) {
return UIApplication.shared.windows
.first?
.windowScene?
.interfaceOrientation
.isLandscape ?? false
} else {
return UIApplication.shared.statusBarOrientation.isLandscape
}
}
}
Usage:
if (UIWindow.isLandscape) {
print("Landscape")
} else {
print("Portrait")
}