I'm trying to add multiple bar button items to each side of a custom nav bar.
I'm using the following method but nothing shows up.
There are a few threads on this in Obj-C but I'm struggling to translate the syntax. Is there a workaround for this in Swift?
@IBOutlet weak var navBar: UINavigationBar!
override func viewDidLoad() {
var iconOne = UIImage(named: "iconOne")
var iconTwo = UIImage(named: "iconTwo")
var buttonOne:UIBarButtonItem = UIBarButtonItem(image: iconOne, style: UIBarButtonItemStyle.Plain, target: self, action: nil)
var buttonTwo:UIBarButtonItem = UIBarButtonItem(image: iconTwo, style: UIBarButtonItemStyle.Plain, target: self, action: nil)
self.navBar.setItems([buttonOne,buttonTwo], animated: true)
}
If you are using a storyboard, drag a Navigation Bar
, and drag a Navigation Item
onto the navigation bar.
Then connect an IBOutlet
for your UINavigationitem
@IBOutlet weak var navItem: UINavigationItem!
override func viewDidLoad() {
super.viewDidLoad()
let navBarButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Bookmarks, target: self, action: nil)
navItem.leftBarButtonItem = navBarButton
}
You can also use code:
let navbar = UINavigationBar(frame: CGRect(x: 0, y: 20,
width: UIScreen.mainScreen().bounds.size.width, height: 50))
navbar.tintColor = .lightGray
self.view.addSubview(navbar)
let navItem = UINavigationItem(title: "Test")
let navBarButton = UIBarButtonItem(barButtonSystemItem: .bookmarks, target: self, action: nil)
navItem.leftBarButtonItem = navBarButton
navbar.items = [navItem]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With