Francesco
Francesco

Reputation: 1047

Default iOS UINavigationBar animation isn't smooth

I'm working on an application and need to hide the UINavigationBar (and toolbar) to provide a fullscreen mode in the in-app browser.

When the app run this code the animation work just fine.

[self.navigationController setNavigationBarHidden:YES animated:YES];
[self.navigationController setToolbarHidden:YES animated:YES];

When I want to exit from the full-screen mode the animation isn't smooth at all.

[self.navigationController setNavigationBarHidden:NO animated:YES];
[self.navigationController setToolbarHidden:NO animated:YES];

During the animation a black rectangle is visible under the navigation bar, I think it is the UIWebView that resize itself (the toolbar animation work just fine.)

Any idea on how I can solve this problem?

Upvotes: 0

Views: 567

Answers (1)

Raginmari
Raginmari

Reputation: 2529

Instead of using setNavigationBarHidden:animated: for hiding the navigation bar, try this:

In your view controller's viewDidLoad compute different frames for your navigation bar and your view:

// The normal navigation bar frame, i.e. fully visible
normalNavBarFrame = self.navigationController.navigationBar.frame;

// The frame of the hidden navigation bar (moved up by its height)
hiddenNavBarFrame = normalNavBarFrame;
hiddenNavBarFrame.origin.y -= CGRectGetHeight(normalNavBarFrame);

// The frame of your view as specified in the nib file
normalViewFrame = self.view.frame;

// The frame of your view moved up by the height of the navigation bar
// and increased in height by the same amount
fullViewFrame = normalViewFrame;
fullViewFrame.origin.y -= CGRectGetHeight(normalNavBarFrame);
fullViewFrame.size.height += CGRectGetHeight(normalNavBarFrame);

When you want to go fullscreen:

[UIView animateWithDuration:0.3
                     animations:^{
                         self.navigationController.navigationBar.frame = hiddenNavBarFrame;
                         self.view.frame = fullViewFrame;
                     } completion:^(BOOL finished) {

                     }];

When you want to return to normal:

[UIView animateWithDuration:0.3
                     animations:^{
                         self.navigationController.navigationBar.frame = normalNavBarFrame;
                         self.view.frame = normalViewFrame;
                     } completion:^(BOOL finished) {

                     }];

Tested this in the iOS 5.1 emulator. Hope you can use that. The "black rectangle" must be the default background color of your window, i.e. a gap between your navigation bar and your view.

Upvotes: 1

Related Questions