6

Hi I'm new in iOS development. I've one main screen with navigation bar hidden true. From there I am navigating to another view using back segue. but when I click back it showing navigation bar on main screen. Here is my problem description.

In main screen onviewload I am doing :

self.navigationController.navigationBarHidden = YES;

once user go to another view using back segue in new controller, I'm doing

self.navigationController.navigationBarHidden = NO;

And now, if I click back it will show navigation bar on main window also which I don't want. Basically I want main screen without navigation bar and next window with navigation bar.

How to do this. Need Help. Thank you.

ChenSmile
  • 3,401
  • 4
  • 39
  • 69
nilkash
  • 7,408
  • 32
  • 99
  • 176

6 Answers6

21

Put that code in viewWillAppear instead of viewDidLoad, and it should work properly.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • The reason - in `viewDidLoad` the `navigationController` is not assigned yet. – Sulthan Apr 15 '14 at 08:54
  • @Sulthan, not true. If you log self.navigationController in viewDidLoad, you will see that it's already present. It's just that the hidden state of the navigation bar is set at the navigation controller level, so if you set it to No in one controller, it will stay that way until you change it in all other controllers. Putting the code in each controller's viewWillAppear method ensures that it gets reset when that controller appears. – rdelmar Apr 15 '14 at 15:12
  • 2
    Hello rdelmar that code is not working either in viewDidLoad or viewWillAppear... any idea.... self.navigationController.navigationBarHidden = YES; – Mubin Mall Jan 26 '15 at 11:53
3
-(void)viewWillAppear:(BOOL)animated
{
  [super viewWillAppear:animated];
  self.navigationController.navigationBarHidden = YES;
}
Duck
  • 34,902
  • 47
  • 248
  • 470
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
1

I have a Tab viewcontroller consist of 4 tabs, one of my tab doesn't need navigationbar, but others need.

None of the previous answers solve my case, these code does.

//隐藏App导航条,使用RN自己的导航条
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
//    self.navigationController.navigationBarHidden = YES;    //这句是  **完全没** 个卵用
//    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}

//恢复App导航条
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
//    self.navigationController.navigationBarHidden = NO;     //这句是  **完全没** 个卵用
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
Jess
  • 620
  • 1
  • 7
  • 18
1

Don't use

self.navigationController.navigationBarHidden = YES;

You should use

self.navigationController.navigationBar.hidden = NO;
BDL
  • 21,052
  • 22
  • 49
  • 55
  • I'm kind of shocked, but this is the only thing that worked in my particular scenario. Thank you. – Grambo Apr 11 '22 at 20:10
1

For Swift 4, add following in viewWillAppear

        self.navigationController?.setNavigationBarHidden(false, animated: false)
Mohsin Qureshi
  • 1,203
  • 2
  • 16
  • 26
0

self.navigationController?.setNavigationBarHidden(false, animated: false)

Put the above line of code in viewWillAppear instead of viewDidLoad.

Miral Kamani
  • 11
  • 1
  • 3