默认iOS UINavigationBar动画不流畅

时间:2012-04-30 12:43:46

标签: ios uinavigationcontroller core-animation

我正在开发一个应用程序,需要隐藏UINavigationBar(和工具栏)以在应用内浏览器中提供全屏模式。

当应用程序运行此代码时,动画工作正常。

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

当我想退出全屏模式时,动画根本不光滑。

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

在动画期间,导航栏下方会显示一个黑色矩形,我认为是自动调整大小的UIWebView(工具栏动画工作正常。)

关于如何解决这个问题的任何想法?

1 个答案:

答案 0 :(得分:1)

不要使用setNavigationBarHidden:animated:隐藏导航栏,请尝试以下操作:

在视图控制器的viewDidLoad中为您的导航栏和视图计算不同的帧:

// 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);

当你想要全屏时:

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

                     }];

当你想恢复正常时:

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

                     }];

在iOS 5.1模拟器中测试过。希望你能用它。 “黑色矩形”必须是窗口的默认背景颜色,即导航栏和视图之间的间隙。