关于leftBarButtonItem不以编程方式显示

时间:2017-06-13 03:27:06

标签: ios swift uinavigationbar uibarbuttonitem

我正在尝试以编程方式在我的View Controller的导航栏中添加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根本没有出现在视图控制器中。我在想,我应该将这个片段代码放在导航控制器中,嵌入了View Controller吗? 如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

使用以下代码:

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