Tuesday, August 21, 2012

Animating UINavigationBar

In this section, i will show you the code for hiding and showing UINavigationBar with smooth animation effect.
Lets say , your UINavigationBar object is  : UINavigationBar *navigationBar;

- (void) animateAndShowNavBar{
    // get the frame of navigation bar
    CGRect navBarFrame = navigationBar.frame;
    // set the beginning y coordinate of the title/navigation bar. Its 0(on the top) for this case
    navBarFrame.origin.y = 0;
 
// Now, to animate use one of the method below:
/* For iOS 4 and later we can use the following code to animate :
[UIView animateWithDuration:0.5 delay:0.0 options: UIViewAnimationCurveEaseOut   animations:^
     {
         navigationBar.frame = navBarFrame;
     }
       completion:^(BOOL finished)
     {
         NSLog(@"Done!");
     }
     ];
/* end of animation for iOS 4 and later ........*/

// else use the code below to animate which is a older way :
/******* older way of animating *******/
   [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
      // set the final position of the bar
    navigationBar.frame = navBarFrame;
       [UIView commitAnimations];
/***** end of older way of animating *****/}
- (void) hideBarWithAnimation{
    // get the frame of our title/navigation bar
    CGRect navBarFrame = navigationBar.frame;
    // set the beginning y coordinate of the title/navigation bar to the current position minus the height of the bar.
    navBarFrame.origin.y = - navBarFrame.size.height;
      [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
       // set the final position of the bar
    navigationBar.frame = navBarFrame;
       [UIView commitAnimations];
}


  

No comments:

Post a Comment