简体   繁体   中英

Hide navigationBar on push not working

I'm trying to push to a viewController, however i wan't to hide the navigationBar in this viewController. However it does not seem to apply even though i've set below before pushing?

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let cameraViewController = ALCameraViewController(croppingEnabled: false) { image in
        // Do something with your image here.
        // If cropping is enabled this image will be the cropped version
    }
    cameraViewController.navigationController?.setNavigationBarHidden(true, animated: false)
    self.navigationController?.pushViewController(cameraViewController, animated: true)


}

the alternate way . you can directly hide/show the navigation bar on cameraViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];   //it hides  
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO];    // it shows
}

--- In swift ---

override func viewWillAppear(animated: Bool) {
    self.navigationController?.navigationBarHidden = true
}

override func viewWillDisappear(animated: Bool) {
    self.navigationController?.navigationBarHidden = false
}

--- Swift 4.0 ---

override func viewWillAppear(_ animated: Bool) {
    self.navigationController?.isNavigationBarHidden = true
}

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

Your code isn't working because trying to access navigationController property when it's equal to nil (quote from docs: "This property is nil if the view controller is not embedded inside a navigation controller.")

So, if you need to hide navigation bar in specified view controller use code from Keyur, or, if you can't modify code of this view controller and can't subclass it, you can hide/show navigation bar inside - navigationController:willShowViewController:animated: in your navigation controller delegate

In the storyboards you need select your ViewController and go to Editor->Embebed In->Navigation Controller. I have two Navigation Controllers, one in the root and another follow my ViewController. And the navigationBarHidden true or false, works perfectly for me, in my case.

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