Show / hide navigation bar with smooth animation

I have a navigation based application. The first view (rootcontroller) begins with only three large buttons. There is no navigation bar. From there, everything else is tables and have navigation bars. I do this to show / hide the navigation bar:

MyAppAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; appDelegate.navigationController.navigationBar.hidden = NO; 

As soon as I leave the root controller, the navigation bar will jump into place and stand on top of the table, instead of pushing it away. He pinches the top of the table. Returning to the root controller is not smooth in the way the navigation bar disappears. Is there a smoother / better way to hide the navigation bar for the root controller only?

+46
objective-c iphone cocoa-touch uinavigationcontroller
Jan 17 '10 at 1:05
source share
3 answers

You can use [navigationController setNavigationBarHidden: YES animated:YES] to hide the panel seamlessly

Reference

+106
Jan 17 '10 at 1:35
source share

This great piece of code enlivens a hidden navigation bar without user interface problems:

[navigationController setNavigationBarHidden: YES animated:YES]

But...

  • Use the self.navigationController.navigationBarHidden property to check the code instead of the self.navigationController.navigationBar.hidden property. This will save you the pain of unexpected user interface positioning issues.
  • Put this method in - - (void) viewWillAppear: (BOOL) animated or later in the view lifecycle. This is recommended because if you do this in - (void) viewDidLoad , for example, you get an ugly black rectangular view from the view that displays the navigation bar for the view that doesn't! For example, if the navigation bar is hidden in your home view, but all of its children have a displayed navigation bar, when you go to the home view, the animation will show a black bar instead of the navigation bar until the animation finishes.
+4
Mar 26 '14 at 9:10
source share

You can customize the animation and duration of the navigation bar in the following ways. It will provide you a callback after the animation is complete.

  // pass a param to describe the state change, an animated flag and a completion block matching UIView animations completion - (void)setNavigationBarVisible:(BOOL)visible animated:(BOOL)animated completion:(void (^)(BOOL))completion { // fail if the current state matches the desired state if ([self navigationBarIsVisible] == visible) return completion(YES); // get a frame calculation ready CGFloat nheight = self.navigationController.navigationBar.frame.size.height; CGFloat noffsetY = (visible)? -nheight : nheight; // zero duration means no animation CGFloat duration = (animated)? 0.3 : 0.0; [UIView animateWithDuration:duration animations:^{ CGRect nframe = self.navigationController.navigationBar.frame; self.navigationController.navigationBar.frame = CGRectOffset(nframe, 0, noffsetY); } completion:completion]; } // know the current state of the navigation bar - (BOOL)navigationBarIsVisible { return self.navigationController.navigationBar.frame.origin.y < CGRectGetMinY(self.view.frame); } // Show or Hide navigation bar [self setNavigationBarVisible:![self navigationBarIsVisible] animated:YES completion:^(BOOL finished) { NSLog(@"navigation bar finished"); }]; 

Before hiding the navigation bar:

Before hiding the navigation bar:

After hiding the navigation bar:

After hiding the navigation bar:

+2
Feb 18 '16 at 9:37
source share



All Articles