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:
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")
}
This is how I do it for iOS13 for Swift 5.1:
var statusBarOrientation: UIInterfaceOrientation? {
get {
guard let orientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation else {
#if DEBUG
fatalError("Could not obtain UIInterfaceOrientation from a valid windowScene")
#else
return nil
#endif
}
return orientation
}
}
Objective C, iOS 13 and compatible with older versions:
+ (BOOL)isLandscape
{
if (@available(iOS 13.0, *)) {
UIWindow *firstWindow = [[[UIApplication sharedApplication] windows] firstObject];
if (firstWindow == nil) { return NO; }
UIWindowScene *windowScene = firstWindow.windowScene;
if (windowScene == nil){ return NO; }
return UIInterfaceOrientationIsLandscape(windowScene.interfaceOrientation);
} else {
return (UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication.statusBarOrientation));
}
}
I added this to a UIWindow
Category.
I came up with the following solution, but am open to improvements or suggestions should I be making some mistake I am not aware of:
var isLandscape: Bool {
return UIApplication.shared.windows
.first?
.windowScene?
.interfaceOrientation
.isLandscape ?? false
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With