关于 leftBarButtonItem 没有以编程方式出现

About leftBarButtonItem doesn't appear programmatically

我正在尝试以编程方式在我的视图控制器的导航栏中添加一个 leftBarButtonItem(此视图控制器嵌入在导航控制器中)。我在 viewDidLoad

中实现了这段代码
let backButton = UIButton(type: .custom)
backButton.setImage(UIImage(named: "BackButton.png"), for: .normal)
backButton.setTitle("Back", for: .normal)
backButton.setTitleColor(backButton.tintColor, for: .normal)
backButton.addTarget(self, action: #selector(self.backAction(_:)), for: .touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)

我运行 测试,但是backButton 根本没有出现在视图控制器中。我在想,我是否应该将这段代码放在嵌入了视图控制器的导航控制器中? 如何解决这个问题?

使用以下代码:

self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "Back")!, style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.actionBack))