Showing/hiding navigation bar with smooth animation

前端 未结 3 1487
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 09:38

I have a navigation based app. The first view (rootcontroller) starts with three large buttons only. No navigationbar. From there, everything else is tableviews and have

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-23 10:14

    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:

提交回复
热议问题