[英]Showing/hiding navigation bar with smooth animation
I have a navigation based app. 我有一个基于导航的应用程序。 The first view (rootcontroller) starts with three large buttons only.
第一个视图(rootcontroller)仅以三个大按钮开头。 No navigationbar.
没有导航栏。 From there, everything else is tableviews and have navigation bars.
从那里,其他一切都是tableviews和导航栏。 I'm doing this to show/hide the navigation bar:
我这样做是为了显示/隐藏导航栏:
MyAppAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.navigationController.navigationBar.hidden = NO;
Once I leave the root controller, the navigation bar will jerk into place and lay on top of the tableview, rather than pushing it down. 一旦我离开根控制器,导航栏就会猛拉到位并放在桌面视图的顶部,而不是将其向下推。 It clips the top part of the tableview.
它剪辑了tableview的顶部。 Going back to the root controller isn't smooth in how the navigation bar disappears.
回到根控制器并不是导航栏消失的方式。 Is there a smoother/better way to do accomplish hiding the navigation bar for the root controller only?
是否有更顺畅/更好的方法来完成隐藏根控制器的导航栏?
This nifty bit of code animates the navigation bar hiding with no UI issues: 这段漂亮的代码动画隐藏了导航栏,没有UI问题:
[navigationController setNavigationBarHidden: YES animated:YES]
You can customize the navigation bar animation and duration by the following methods. 您可以通过以下方法自定义导航栏动画和持续时间。 It will provide you callback once animation will be completed.
一旦动画完成,它将为您提供回调。
// 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");
}];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.