Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hide statusbar and navigationbar simultaneously, like in Pictures application

I have a view with a lot of text in it, so I want to allow user to hide statusBar+navigationBar on single tap. I really like the hiding style in Pictures app, where statusBar and navigationBar hide together(not-sliding, just fading out), with some animationDuration, so I tried to do something like that. Here is what I do in touchesDidBegan method:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    [UIView setAnimationDuration:0.5];
[UIView beginAnimations:@"" context:nil];
    [[UIApplication sharedApplication] setStatusBarHidden:!([UIApplication sharedApplication].statusBarHidden) withAnimation:UIStatusBarAnimationNone];
    [self.navigationController setNavigationBarHidden:(!self.navigationController.navigationBarHidden) animated:NO];
    [UIView commitAnimations];
    self.navigationController.navigationBar.translucent = !self.navigationController.navigationBar.translucent; // this is needed to make bars appear on top of my view.
}

But this doesn't simultaneously hide the bars. It makes them slide down. It has the same effect as this version of the method above:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    // deleted UIView animation, changed animation type to "slide"
    [[UIApplication sharedApplication] setStatusBarHidden:!([UIApplication sharedApplication].statusBarHidden) withAnimation:UIStatusBarAnimationSlide];
    // enabled animation for navBar
    [self.navigationController setNavigationBarHidden:(!self.navigationController.navigationBarHidden) animated:YES];
    self.navigationController.navigationBar.translucent = !self.navigationController.navigationBar.translucent; // this is needed to make bars appear on top of my view.
}

If I get rid of UIView animation and hide bars with no animation, they DO hide and appear simultaneously, but TOO fast. Maybe I am going in a wrong direction. Would appreciate if someone could help me out with this.

Edit: got it work

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // don't forget to set navigationBar.translucent to YES
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    [UINavigationBar setAnimationDuration:3.0];

    [UINavigationBar beginAnimations:@"" context:nil];
    [[UIApplication sharedApplication] setStatusBarHidden:!([UIApplication sharedApplication].statusBarHidden) withAnimation:NO];
    if ([UIApplication sharedApplication].isStatusBarHidden)
        [self.navigationController.navigationBar setAlpha:0.0];
    else [self.navigationController.navigationBar setAlpha:1.0];
    [UINavigationBar commitAnimations];
}
like image 229
AzaFromKaza Avatar asked Mar 05 '13 04:03

AzaFromKaza


2 Answers

For hide UIStatusBar with animation:

[[UIApplication sharedApplication] setStatusBarHidden: YES withAnimation: UIStatusBarAnimationSlide];

For hide UINavigationBar with animation:

[UINavigationBar beginAnimations:@"NavBarFade" context:nil];
self.navigationController.navigationBar.alpha = 1;
[self.navigationController setNavigationBarHidden:YES animated:NO]; //Animated must be NO!
[UINavigationBar setAnimationCurve:UIViewAnimationCurveEaseIn]; 
[UINavigationBar setAnimationDuration:1.5];
self.navigationController.navigationBar.alpha = 0;
[UINavigationBar commitAnimations];
like image 117
iPatel Avatar answered Oct 22 '22 19:10

iPatel


check this demo of https://github.com/kirbyt/KTPhotoBrowser in this you find how to hide and show statusbar and Navigation bar.

  • mostly you need to use NSTimer for setting auto hide after 4 or 5 second Hidding Statusbar or Navigation bar

  • you can also tagged this Timer using Touch Begun to show or Hide randomly.

Hope its help's you for Doing your task. check Sample Demo of above Github link using Barack point. you can easly find the function of hide and show Statusbar or NavigationBar.

like image 43
Nitin Gohel Avatar answered Oct 22 '22 20:10

Nitin Gohel