Push / Pop View Controller With Navigation Bar from View Controller Without Navigation Bar

后端 未结 2 1634
广开言路
广开言路 2020-12-08 16:00

I\'m trying to push a view controller with a visible navigation bar from a view controller with a hidden navigation bar.

I tried all sorts of combinations of

相关标签:
2条回答
  • 2020-12-08 16:47

    It is possible without hacking together a solution by yourself. Here is what you do:

    Your root viewController:

    @implementation ViewController
    
    ....
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        [self.navigationController setNavigationBarHidden:YES animated:animated];
    }
    
    @end
    

    And the pushed viewController:

    @implementation SecondViewController
    
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    
        [self.navigationController setNavigationBarHidden:NO animated:animated];
    }
    
    @end
    

    This will do. It also keeps the interactive transition working ;)

    I find it disturbing, however, that this type of functionality is not documented at all by apple. - You can also hide and show toolbars with these 'call-points' (inside viewWillAppear:)

    EDIT

    I just realized that this is the same code you wrote in your question. Please test it again. I am 100% sure that this works - I used this functionality in one of my apps, too.

    Please also note that my code does use animated:animated instead of your animated:NO. This may be the crucial point here :)

    0 讨论(0)
  • 2020-12-08 16:56

    I just set up two view controllers to test this back and forth.

    @interface VC1 ()
    
    @end
    
    @implementation VC1
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    }
    
    @end
    

    and a second

    #import "ViewControllerTwo.h"
    
    @interface ViewControllerTwo ()
    
    @end
    
    @implementation ViewControllerTwo
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        self.navigationController.navigationBarHidden = NO;
    }
    
    @end
    

    VC1 is embedded in a navigationController (which is the root controller for the app), with a button that navigates to ViewControllerTwo. I have a push segue from VC1 -> ViewControllerTwo, this method works. When I tap on the button, the view controller is visible on ViewControllerTwo, when I press back, the navigationBar is gone.

    0 讨论(0)
提交回复
热议问题