5

I am trying to follow this tutorial, this answer, and this answer to create navigation bars for each of my tabs in a tab-based application in iOS 8 / Swift, but no title or buttons on my navigation bar are showing.

Here is what I have so far:

// AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {            
    let tabBarController = UITabBarController()

    let vc1 = ViewController()
    let vc2 = ViewController()
    let vc3 = ViewController()

    let nc1 = UINavigationController(rootViewController: vc1)
    let nc2 = UINavigationController(rootViewController: vc2)
    let nc3 = UINavigationController(rootViewController: vc3)

    let controllers = [nc1,nc2,nc3]

    tabBarController.viewControllers = controllers

    nc1.tabBarItem = UITabBarItem(title: "item1", image: nil, tag: 1)
    nc2.tabBarItem = UITabBarItem(title: "item2", image: nil, tag: 1)
    nc3.tabBarItem = UITabBarItem(title: "item3", image: nil, tag: 1)

    window = UIWindow(frame: UIScreen.mainScreen().bounds)
    window?.rootViewController = tabBarController
    window?.makeKeyAndVisible()

    return true
}

// ViewController.swift
class ViewController: UIViewController, UINavigationBarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))
        navigationBar.backgroundColor = UIColor.blueColor()
        navigationBar.delegate = self;

        let navigationItem = UINavigationItem()
        navigationItem.title = "Title"

        let leftButton =  UIBarButtonItem(title: "Left Button", style:   UIBarButtonItemStyle.Plain, target: self, action: nil)
        let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)

        navigationItem.leftBarButtonItem = leftButton
        navigationItem.rightBarButtonItem = rightButton

        navigationBar.items = [navigationItem]

    }

    func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
        return UIBarPosition.TopAttached
    }
}

But I am just getting a blank navigation bar on top in the simulator.

iPhone 6 simulator screenshot

Community
  • 1
  • 1
Imran
  • 12,950
  • 8
  • 64
  • 79
  • 1
    for the title did you try `self.title = "Your Title"` – Alaeddine Jul 15 '15 at 17:05
  • It looks like my navigation bar is partially under the status bar. When I play with the dimensions I can see the title and buttons sticking out the bottom. – Imran Jul 15 '15 at 17:11
  • Aladin, you were on the right track. I was creating two navigation bars. The navigation controller comes with a navigation bar and I needed to set the title with self.navigationItem.title = "My Title". – Imran Jul 15 '15 at 17:32
  • 1
    That's what I couldn't understand, I was wondering why you was creating a new navigation bar while your view controller is embed already in one navigation controller who has already a bar – Alaeddine Jul 15 '15 at 17:35

3 Answers3

6

You're making a custom UINavigationBar when one is already provided to you with the UINavigationController.

Try this instead in your ViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Title"

    let navigationBar = navigationController!.navigationBar
    navigationBar.tintColor = UIColor.blueColor()

    let leftButton =  UIBarButtonItem(title: "Left Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
    let rightButton = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)

    navigationItem.leftBarButtonItem = leftButton
    navigationItem.rightBarButtonItem = rightButton
}
mbottone
  • 1,219
  • 9
  • 13
2

No need to use a new navigation bar just use your existent navigation bar. Remember what you did here :

let nc1 = UINavigationController(rootViewController: vc1)

So your view controller is already embed inside a navigation controller just use self to access to the navigation items

self.title = "Your Title"

var homeButton = UIBarButtonItem(title: "LeftButton", style: .Plain, target: self, action: "")

var logButton = UIBarButtonItem(title: "RigthButton", style: .Plain, target: self, action: "")

self.navigationItem.leftBarButtonItem = homeButton
self.navigationItem.rightBarButtonItem = logButton
Alaeddine
  • 6,104
  • 3
  • 28
  • 45
0

I was unnecessarily creating two navigation bars. The navigation controller comes with a navigation bar, and I changed my ViewController to:

class ViewController: UIViewController, UINavigationBarDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "My Title"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Left Button", style:   UIBarButtonItemStyle.Plain, target: self, action: nil)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
    }

    func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
        return UIBarPosition.TopAttached
    }

}
Imran
  • 12,950
  • 8
  • 64
  • 79