I'm having a lot of troubles with the new auto hiding of the status bar in iOS 8.
In my app, I've got a view in which when the user taps once, the navigation bar and the status bar disappear.
When in landscape, the status bar hides by itself and it's fine to me. I only need it in portrait mode.
But the problem is that when the device is landscape and the bar are shown, when the user taps twice to toggle the bar (so showing), and turns the device in portrait mode, the status bar is still hidden.
Basically I need to be able to hide the statusBar without interfere with its natural behavior on iOS 8, so I recap the scenario:
MRW >
(source: mshcdn.com)
I've tried to adjust the statusBar on willRotate, but I made a mess in which the statusBar would be visible when it wasn't supposed to. Code I'm using:
- (BOOL)prefersStatusBarHidden
{
return statusBarHidden;
}
-(void)toggleBars:(UITapGestureRecognizer *)gesture{
CATransition *animation = [CATransition animation];
animation.type = kCATransitionFromBottom;
animation.subtype = kCATransitionFromTop;
animation.duration = .2f;
[animation setTimingFunction:[CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]];
BOOL toggleNavigationBar = self.navigationController.navigationBarHidden;
[self.navigationController.navigationBar.layer addAnimation:animation forKey:nil];
[self.navigationController setNavigationBarHidden:!toggleNavigationBar animated:YES];
BOOL toggleTabHidden = self.tabBarController.tabBar.hidden;
if(![[[NSUserDefaults standardUserDefaults] objectForKey:@"showTabBar"]isKindOfClass:[NSNull class]]){
if([(NSNumber*)[[NSUserDefaults standardUserDefaults] objectForKey:@"showTabBar"]boolValue])
{
[self.tabBarController.tabBar.layer addAnimation:animation forKey:nil];
[self.tabBarController setHideTabBar:!toggleTabHidden animated:YES];
}
}
statusBarHidden = [UIApplication sharedApplication].statusBarHidden;
[[UIApplication sharedApplication] setStatusBarHidden:!statusBarHidden withAnimation:UIStatusBarAnimationSlide];
[self setNeedsStatusBarAppearanceUpdate];
if (IS_IOS8){
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
if (statusBarHidden){
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
}
}
}
I was thinking about setting a flag in which when the statusBar is hidden when in landscape and all the controls are there, on rotation it would trigger the statusBar. With no success, apparently..
Any help highly appreciated.
Are you using UIViewController-based status bar appearance?
If you implement prefersStatusBarHidden
I assume you are.
Now,
[[UIApplication sharedApplication] setStatusBarHidden:!statusBarHidden withAnimation: UIStatusBarAnimationSlide];
is not supposed to work with UIViewController-based status bar appearance.
You need just to return different value from prefersStatusBarHidden
method and call setNeedsStatusBarAppearanceUpdate
to notify the app that returning value has changed.
So to change statusbar visibility you should just do
@property (nonatomic, assign) BOOL hideStatusBar;
- (BOOL)prefersStatusBarHidden
{
return self.hideStatusBar;
}
- (void)toggleBars:(UITapGestureRecognizer *)gesture
{
... hide navbar and tabbar ...
self.hideStatusBar = ! self.hideStatusBar;
[self setNeedsStatusBarAppearanceUpdate];
}
And that's it!