Storyboard - Hiding top bar of navigation controll

2019-01-31 19:25发布

I'm using a storyboard and I'm trying to hide a top bar of my main navigation controller when a certain button is pressed (or function is called). I know I have to initialize an object referring to a navigation controller from a storyboard (using identifiers), but when I send the setNavigationBarHidden message to this newly created object nothing really happens on screen.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UINavigationController *navController = (UINavigationController*) [storyboard instantiateViewControllerWithIdentifier:@"MyNavController"];

[navController setNavigationBarHidden:YES animated:YES];

Does anyone know what the problem is?

8条回答
Anthone
2楼-- · 2019-01-31 19:48

in swift 4:

self.navigationController!.navigationBar.isHidden = true

something like this:

override func viewDidLoad() {
        super.viewDidLoad()
       self.navigationController!.navigationBar.isHidden = true
    }
查看更多
Rolldiameter
3楼-- · 2019-01-31 19:52

The answers are correct. Just want to add that you probably want to put back the navigation bar when leaving the view as other views might use it and when you hide for one its hidden for all.

override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationController?.navigationBar.isHidden = true
    }

and before you leave the view:

override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)
      self.navigationController?.navigationBar.isHidden = false
}

`

查看更多
登录 后发表回答