'UIApplicationDidChangeStatusBarOrientationNotification is deprecated, any alternative?
I have changed my app target to IOS 13 to check the deprecated methods in my app and I am getting below warning:
'UIApplicationDidChangeStatusBarOrientationNotification' is deprecated: first deprecated in iOS 13.0 - Use viewWillTransitionToSize:withTransitionCoordinator: instead.
Here is the code which I have implemented in my project.
- (instancetype)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onChangeStatusBarOrientationNotification:)
name:UIApplicationDidChangeStatusBarOrientationNotification
object:nil];
}
return self;
}
Please suggest me the solution for ios 12 and ios 13.
Thanks in advance.
Solution 1:[1]
Though this is an old post, I also faced this situation recently. Just implement the protocol UIContentContainer
@interface MyViewController : NSObject <UIContentContainer>
And implement the method in the implementation and within the method add call your function.
@implementation MyViewController {
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[self onChangeStatusBarOrientationNotification];
}
@end
Swift
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
// Put code to be run BEFORE the rotation here...
coordinator.animate(alongsideTransition: nil) { _ in
// Put code to be run after the rotation,
// or after frame-size change here...
}
}
Some other useful checks:
if UIDevice.current.orientation.isLandscape {
print("Landscape")
}
if UIDevice.current.orientation.isFlat {
print("Flat")
} else {
print("Portrait")
}
Solution 2:[2]
Try something like:
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(onChangeStatusBarOrientationNotification:)
name: UIDeviceOrientationDidChangeNotification
object: nil];
But note that it just works and I never call below:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
So, maybe it's enabled by default.
Swift 5
NotificationCenter.default.addObserver(self,
selector: #selector(onLayoutChange),
name: UIDevice.orientationDidChangeNotification,
object: nil)
// ...
@objc public func onLayoutChange() {
// Handle change here.
}
Solution 3:[3]
I have the same problem, and here is what worked for me:
In my UIViewController I had to override the viewWillTransition function and voila.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to:size, with: coordinator)
let before = self.view.window?.windowScene?.interfaceOrientation
coordinator.animate(alongsideTransition: nil) { _ in
let after = self.view.window?.windowScene?.interfaceOrientation
if before != after {
// rotation detected, call you rotation handler here
}
}
}
Source: https://www.biteinteractive.com/dealing-with-ios-13-deprecations/
Mahmud Ahsan's answer is good just that is missing the detection code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Top-Master |
Solution 2 | |
Solution 3 |