简体   繁体   English

显示/隐藏带有平滑动画的导航栏

[英]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? 是否有更顺畅/更好的方法来完成隐藏根控制器的导航栏?

You can use [navigationController setNavigationBarHidden:YES animated:YES] to hide the bar smoothly. 您可以使用[navigationController setNavigationBarHidden:YES animated:YES]平滑地隐藏栏。

Reference 参考

This nifty bit of code animates the navigation bar hiding with no UI issues: 这段漂亮的代码动画隐藏了导航栏,没有UI问题:

[navigationController setNavigationBarHidden: YES animated:YES]

But... 但...

  1. Use the self.navigationController.navigationBarHidden property for checks in the code instead of the self.navigationController.navigationBar.hidden property. 使用self.navigationController.navigationBarHidden属性检查代码而不是self.navigationController.navigationBar.hidden属性。 This will save you a lot of pain from unexpected UI positioning problems. 这将为您带来意外的UI定位问题带来的痛苦。
  2. Take care to place this method in - (void)viewWillAppear:(BOOL)animated or later in the view lifecycle. 注意将此方法置于- (void)viewWillAppear:(BOOL)动画或稍后在视图生命周期中。 This is recommended because if you do it in - (void)viewDidLoad for instance, you will get an ugly black rectangular view during animations from a view which displays its navigation bar to a view which doesn't! 这是推荐的,因为如果你在- (void)viewDidLoad中执行它,你会在动画期间从一个视图显示一个丑陋的黑色矩形视图,该视图将其导航栏显示到一个不显示的视图! For example, if your home view has its navigation bar hidden but all its children have the navigation bar shown, when you pop to home view, the animation will show a black bar in place of the navigation bar until the animation completes 例如,如果您的主视图隐藏了其导航栏但其所有子项都显示了导航栏,则当您弹出到主视图时,动画将显示黑色条代替导航栏,直到动画完成

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");
    }];

Before hide a Navigation bar: 在隐藏导航栏之前:

在隐藏导航栏之前:

After hide a Navigation bar: 隐藏导航栏后:

隐藏导航栏后:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM