Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is correct way to hide/unhide nav bar during an interactive transition via NavigationControllerDelegate?

This is related to another question of mine, iOS 8 + interactive transition + nav bar shown = broken?, but is different.

On iOS 8, when one is doing an interactive transition from view A to view B via the NavigationControllerDelegate / UIViewControllerInteractiveTransitioning method, and view A has a navbar, and view B does NOT, then what is the correct method to hide / unhide the nav bar?

I tried to do this in the ViewController like this:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[self transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        if (self.navigationController) {
            [self.navigationController setNavigationBarHidden:YES animated:animated];
        }

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        NSArray *debugViews = [context containerView].subviews;
        NSLog(@"%@", debugViews);

        if ([context isCancelled] ) {
            if( self.navigationController ) {
                [self.navigationController setNavigationBarHidden:NO animated:animated];
            }
        }
    }];
}

- (void)viewWillDisappear:(BOOL)animated {

    [[self transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        if (self.navigationController) {
            [self.navigationController setNavigationBarHidden:NO animated:animated];
        }

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        if ([context isCancelled] ) {
            if( self.navigationController ) {
                [self.navigationController setNavigationBarHidden:YES animated:animated];
            }
        }
    }];

    [super viewWillDisappear:animated];
}

... but there are two big problems:

  1. The view (mainly the navbar) "flickers" sometimes when animation is completing. This is really ugly if you have a complex view underneath.

  2. If the user cancels the interactive transition (ie. by not dragging far enough or pinching enough) then the navbar goes away forever, even though I can see in the code that it's being told to unhide.

Here is some soure-code to show this happening: https://github.com/xaphod/UIViewControllerTransitionTut

--> un-pinch to go from one view controller to another; the first view has a nav bar, the second one does not. When you complete the transition, you can sometimes see flickering (problem 1 above). When you un-pinch just a little bit and let go, that's a cancelled transition: although you're still on view 1, the navbar has disappeared (problem 2 above).

like image 695
xaphod Avatar asked Jul 21 '15 12:07

xaphod


People also ask

How do I make my navigation bar hidden?

On Android 4.1 and higher, you can set your application's content to appear behind the navigation bar, so that the content doesn't resize as the navigation bar hides and shows. To do this, use SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION .

How do I customize the navigation bar in Swift?

Go to the ViewController. swift file and add the ViewDidAppear method. a nav helper variable which saves typing. the Navigation Bar Style is set to black and the tint color is set to yellow, this will change the bar button items to yellow.

How do I change the navigation bar style?

From Settings, tap Display. Tap Navigation bar, and then choose Swipe gestures.


1 Answers

The right way to hide the navigation bar will be to use Navigation's controller delegate,make sure you set the window's navigation controller delegate to self before using the following delegate method:-

Just do this in the AppDelegate.m

- (BOOL)application:(UIApplication *)application 
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
        self.window.rootViewController.navigationController.delegate=self;

    //do your rest code...


  }

-(void)navigationController:(UINavigationController *)navController 
             willShowViewController:(UIViewController *)viewController 
                           animated:(BOOL)animated 
{
            [navController setNavigationBarHidden:([viewController isKindOfClass:[CustomViewController class]]) 
                              animated:animated];   // just mention the view controller class type for which  you want to hide 
}

Referred from this SFO's

like image 79
Vizllx Avatar answered Oct 14 '22 15:10

Vizllx