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 butt
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
}
}
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
}
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