Skip to content

Instantly share code, notes, and snippets.

@garymansted
Last active August 6, 2016 07:16
Show Gist options
  • Save garymansted/33ae658b18f9daa344c80e1794ffccda to your computer and use it in GitHub Desktop.
Save garymansted/33ae658b18f9daa344c80e1794ffccda to your computer and use it in GitHub Desktop.
Add two bar button items on the one side with images SWIFT
//Create button objects
var barButton1 = UIBarButtonItem()
var barButton2 = UIBarButtonItem()
//Create button instances and images in viewDidLoad()
let image1 = UIImage(named: "HelloWorldImage1")!
let image2 = UIImage(named: "HelloWorldImage2")!
barButton1 = UIBarButtonItem(image: image1, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ExampleVC.function1))
barButton2 = UIBarButtonItem(image: image2, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ExampleVC.function2))
//Add buttons to navigation bar - still in viewDidLoad()
self.navigationItem.setRightBarButtonItems([barButton1,barButton2], animated: true)
//Targets
func function1() {
print("Hello World 1")
}
func function2() {
print("Hello World 2")
}
@garymansted
Copy link
Author

garymansted commented Aug 4, 2016

Copy and paste code. Drop it in your viewDidLoad. Remember to set your images before adding the buttons. Don't forget to change the ExampleVC to your ViewController's name. Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment