简体   繁体   中英

UIViewController pushViewController, show navigationBar

I have UIViewController(1) without navigationBar, and I need to push anouther UIViewController(2) that have navigationBar, and when I click Back on it, navigationBar must hide on 1 controller. I have tried uiviewcontroller delegates. But nothing is working..

Please help..

This will show the navbar on the second screen:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];        
    self.navigationController.navigationBarHidden = NO;
}

You will also need to hide the navbar when you return to the first screen:

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];        
    self.navigationController.navigationBarHidden = YES;
}

I think you want the animated option. If you roll with the approaches above ( self.navigationController.navigationBarHidden = value ) you get some undesirable visual crumbs.

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

    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

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

    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

This will push/pop smoothly with the navBar firmly attached to the appearing/disappearing view.

Place this code in first view controller

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];        
    self.navigationController.navigationBarHidden = YES;
}

Answers from Alfie or Ader would be a disaster if you have many viewcontrollers' navigation bar hidden/show state to manage.

I just have posted code dedicated on UINavigationBar appearance management on github. check out RRViewControllerExtension , it will solve your problem gracefully.

With RRViewControllerExtension , you even don't have to #import the header file, all you have to do is just override any desired method below in your viewcontroller.

//override any of the methods below in your viewcontroller's .m file to make specific navigation bar appearance

-(BOOL)prefersNavigationBarHidden;
-(BOOL)prefersNavigationBarTransparent;

-(nullable UIColor *)preferredNavatationBarColor;
-(nullable UIColor *)preferredNavigationItemColor;
-(nullable UIImage *)preferredNavigationBarBackgroundImage;
-(nullable NSDictionary *)preferredNavigationTitleTextAttributes;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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