peter peter - 2 years ago 135
Swift Question

swift navigation bar settings inside a function

Im trying to process a navigation inside a reusble func i cannot get it to work this is the code i have so far.

static func navigationBarShow() {

let navigationCoontroller = UINavigationController()
navigationCoontroller.navigationBarHidden = false

navigationCoontroller.navigationBar.barTintColor = UIColor(red:0.4, green:0.76, blue:0.93, alpha:1.0)
navigationCoontroller.navigationBar.translucent = false
navigationCoontroller.title = "Signup"

navigationCoontroller.navigationBar.tintColor = UIColor.whiteColor()
navigationCoontroller.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]


Answer Source

Try writing it as an extension to UINavigationController and use self as the navigation controller. You are creating a new UINavigationController instance each time you want to display a navigation bar and that instance is never being displayed on the screen.

extension UINavigationController {

    func showNavigationBar() {

        self.navigationBarHidden = false

        self.navigationBar.barTintColor = UIColor(red:0.4, green:0.76, blue:0.93, alpha:1.0)
        self.navigationBar.translucent = false
        self.title = "Signup"

        self.navigationBar.tintColor = UIColor.whiteColor()
        self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]



Then, from any UINavigationController, you can call showNavigationBar().


In the code for any subclass of UIViewController that is embedded in a UINavigationController, call navigationController!.showNavigationBar().

For example:

class MyViewController: UIViewController {

    override func viewDidLoad() {


Note that the ! assumes your view controller is embedded in a navigation controller. If any vc is not embedded in a navigation controller but you want a navigation bar, you should consider embedding it in a navigation controller.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download