UIBarButtonItem with Image Example in Swift

In this blog post, you will learn how to create a UIBarButtonItem with Image in Swift. It is going to be a very short and simple Swift code example, that demonstrates how to:

  • Create UIBarButtonItem programmatically,
  • Create UIImage programmatically,
  • Add image to UIBarButtonItem,
  • Add target action to UIBarButtonItem to call a local function.
import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        // Image needs to be added to project.
        let buttonIcon = UIImage(named: "left_side")
        let leftBarButton = UIBarButtonItem(title: "Edit", style: UIBarButtonItem.Style.done, target: self, action: #selector(self.myLeftSideBarButtonItemTapped(_:)))
        leftBarButton.image = buttonIcon
        self.navigationItem.leftBarButtonItem = leftBarButton
    @objc func myLeftSideBarButtonItemTapped(_ sender:UIBarButtonItem!)
    override func didReceiveMemoryWarning() {

For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.

Leave a Reply

Your email address will not be published.