Amsheer
Amsheer

Reputation: 7141

Navigation bar not showing iOS swift

I am having multiple view controller in my application. I want to hide navigationbar in my first view controller. So I use the following code to hide the navigation bar

navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true);

Now I want to add navigation bar in some other viewController but, my navigation bar not visible in that viewcontroller. Why it is happening?

My storyboard showing the navigation bar but once I try to run my application it is gone.

If I hide navigation bar from one view controller then we can't use navigation controller, Is it so? I hope I am wrong. Then what are the reasons for navigation bar not shown?

EDIT:

Also I want my view controller in portrait mode only. So I did the following Is that causing the issue?

extension UINavigationController{
    public override func shouldAutorotate() -> Bool {
        if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
            UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
            UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
                return false
        }
        else {
            return true
        }
    }
    
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown]
    }
    

}

Edit 1:

I am using following code to move from one view controller not link from the storyboard. Is that causing issue now?

 let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let secondViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
        presentViewController(secondViewController, animated: false, completion: nil)

Edit 2:

Please check my following screenshots. Which are my settings for secondview controller

enter image description here

enter image description here

Edit 3:

Here is my navigation controller attribute inspector enter image description here

Upvotes: 30

Views: 85003

Answers (8)

hari ys
hari ys

Reputation: 1

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    let navigationBarAppearace = UINavigationBar.appearance()
    //           navigationBarAppearace.tintColor = .tintColor
    navigationBarAppearace.barTintColor = UIColor(displayP3Red: 47/255, green: 54/255, blue: 64/255, alpha: 1.0)
    navigationBarAppearace.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    navigationBarAppearace.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    
    if #available(iOS 13.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.backgroundColor = UIColor(displayP3Red: 47/255, green: 54/255, blue: 64/255, alpha: 1.0) //<insert your color here>
        navigationBarAppearace.standardAppearance = navBarAppearance // have a look here ;). Required to solve navigation bar appearing only on scrolling.
        navigationBarAppearace.scrollEdgeAppearance = navBarAppearance
    }
    return true
}

// and add this in your viewController, ViewDidLoad

self.navigationController?.navigationBar.prefersLargeTitles = true

// This worked for me in Dec 2025, Xcode 16.

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82756

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // Hide the navigation bar on the this view controller
    self.navigationController?.setNavigationBarHidden(true, animated: animated)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Show the navigation bar on other view controllers
    self.navigationController?.setNavigationBarHidden(false, animated: animated)
}

Upvotes: 25

Vittal Pai
Vittal Pai

Reputation: 3563

do like in viewcontroller based hidden using Swift 4.0

To hide navigationController in viewWillAppear

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

To unhide navigationController in viewWillDisappear

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

Upvotes: 6

Ashfaque
Ashfaque

Reputation: 1284

It's too late to reply and there are other good answers but I would like to share what worked for me.

let controller = self.storyboard?.instantiateViewControllerWithIdentifier("HomeVC")
    self.navigationController!.pushViewController(controller!, animated:true)

Upvotes: 0

Makalele
Makalele

Reputation: 7551

Navigation Controller is a controller, which has stack of view controllers. So if you have something like this:

NAV -> A -> (segue) B

Even if you'll hide navigation bar you still should be able to make segues. Also can't you just unhide navigation bar in second (B) view controller in viewWillAppear? And in first the same way hide it on viewWillAppear.

edit: Final solution to the problem: Use:

 let controller = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
 self.navigationController!.pushViewController(controller) 

instead of:

let secondViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC")
presentViewController(secondViewController, animated: false, completion: nil)

Because pushViewController will add secondViewController to its stack. presentViewController was replacing your navigation controller that's why you couldn't see navigation bar.

Upvotes: 39

harsha yarabarla
harsha yarabarla

Reputation: 506

in viewDidLoad method of the view controller in which you don't want to show navigation bar add the line

navigationController.navigationBarHidden = true

you are presently hiding in all view controllers

Edit: You are presenting view controller instead it should be

self.navigationController!.pushViewController(controller) 

Upvotes: 7

swiftBoy
swiftBoy

Reputation: 35783

I am having same requirement in my swift project.

this is how I have handled Navigation bar

Make sure your first screen is embedded into Navigation controller

enter image description here

example we have two screens A and B

In screen A you need to hide navigation bar in viewWillAppear

override func viewWillAppear(animated: Bool)
    {
         super.viewWillAppear(animated)

        //hide navigation for screen A
        self.navigationController?.navigationBarHidden = true
    }

for enabling Navigation in screen B you need to add below code in screen A

override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)
    {
        if (segue.identifier == "screen B's segue identifier here")
        {
           //enable navigation for screen B       
            navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true)
        }

    }

Using above style, I can enable or disable navigation bar for specific screen, whenever I want

Upvotes: 2

sunshinejr
sunshinejr

Reputation: 4854

If you need to have this navigation bar hidden only in this controller, the best way is to show it in viewWillDisappear() and hide in viewWillAppear().

Upvotes: 1

Related Questions