我正试图以编程方式在视图控制器的导航栏中添加一个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))

关于ios - 关于leftBarButtonItem不会以编程方式显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44511864/

10-11 02:21