The problem:
Navigation bar is under status bar after playing video in landscape mode.
The application:
Setups to reproduce:
Screenshots:
Swift 3
In the presenting view controller, override the prefersStatusBarHidden
property to only hide the status bar if it's in landscape.
override var prefersStatusBarHidden: Bool {
return UIApplication.shared.statusBarOrientation.isLandscape
}
Then add an observer for when the device is rotated.
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(videoDidRotate), name: .UIDeviceOrientationDidChange, object: nil)
}
In the observer's method, call setNeedsStatusBarAppearanceUpdate
:
func videoDidRotate() {
self.setNeedsStatusBarAppearanceUpdate()
}
That should do it.
It's very simple,
swift 3
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews();
UIApplication.shared.isStatusBarHidden = false
}
@Aaron answer almost works, there's only one problem: when you tap "done" in the video, while still holding the device in landscape orientation, it won't show status bar until you'll rotate your device back into portrait.
In that case, I've added notification observer when "done" button is tapped and then I switch to portrait programmatically.
My code is in Objective C:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoDidRotate) name:UIDeviceOrientationDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closedFullScreen:) name:UIWindowDidBecomeHiddenNotification object:nil];
}
-(void)closedFullScreen:(NSNotification *)myNotification{
[[UIDevice currentDevice] setValue:
[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
forKey:@"orientation"];
}
- (BOOL)prefersStatusBarHidden {
return UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
}
- (void)videoDidRotate {
[self setNeedsStatusBarAppearanceUpdate];
}
EDIT:
View controller-based status bar appearance in .plist file must be set to YES.
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