swift 4 中未显示自定义 leftBarButtonItem

Custom leftBarButtonItem not showing in swift 4

我正在尝试在导航项中添加自定义 lefBarButton 但它没有显示任何东西 我尝试了这些答案

navigation bar button not showing in swift 3

navigation bar button and items not showing in swift 3

Navigation bar button not showing

我的视图控制器的 segue 类型是 show。

这是我的代码

    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)

我花了几个小时来修复它。 有没有人知道。

这是后退按钮

问题出在这一行。

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

imageView is a read-only property.

如果要设置图片,应该这样设置。

leftButton.setImage(UIImage("back-icon"), for: .normal) // Make sure your image is actually named "back-icon"