2

i'm trying to add custom lefBarButton in navigation item but it not showing anything i tried these answers

https://stackoverflow.com/questions/48564480/custom-leftbarbuttonitem-not-showed-in-ios-11

navigation bar button not showing in swift 3

navigation bar button and items not showing in swift 3

Navigation bar button not showing

Bar button item not shown in navigationBar

Navigation bar not showing iOS swift

Swift 3 - Why is my Navigation Bar not showing?

iOS 8 Swift navigation bar title, buttons not showing in tab based application

segue type for my view controller is show.

here is my code

    let leftButton = UIButton(type: .roundedRect)
    leftButton.addTarget(self, action: #selector(backClick), for: .touchUpInside)
    leftButton.setTitle("", for: .normal)
    leftButton.tintColor = UIColor.red
    leftButton.imageView?.image = UIImage(named: "back-icon")
    leftButton.frame = CGRect(x: 0, y: 0, width: 22, height: 15)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: leftButton)

i spend hours to fix it. does anyone have have any idea.

enter image description here

here is the back button enter image description here

1 Answers1

2

The problem is in this line.

leftButton.imageView?.image = UIImage(named: "back-icon")

imageView is a read-only property.

If you want to set an image, you should set it like this.

leftButton.setImage(UIImage("back-icon"), for: .normal) // Make sure your image is actually named "back-icon"
Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50