我尝试在导航控制器中添加自定义后退按钮。我创建了这样的按钮
func setupBackButton() {
let backButton = UIButton.init(type: .custom)
backButton.addTarget(self, action: #selector(backButtonHandler(_:)), for: .touchUpInside)
backButton.setTitleColor(UIColor.white, for: .normal)
backButton.setTitle("<", for: .normal)
backButton.tintColor = UIColor.white
let barBackButton = UIBarButtonItem(customView: backButton)
self.navigationItem.leftBarButtonItem = barBackButton
}
问题是正在添加按钮(我可以点击它并且正在执行按钮上的操作)但是我看不到控制器中的按钮 - 它是不可见的。
控制器的颜色为蓝色。
答案 0 :(得分:1)
你错过了按钮的框架
backButton.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
别忘了添加动作处理程序
func backButtonHandler(_ sender : UIButton) {
}
答案 1 :(得分:0)
试试这个:
func setupBackButton(){
let backBtn : UIBarButtonItem = UIBarButtonItem(title: "<", style: UIBarButtonItemStyle.plain, target: self, action:#selector(backToPreviousVC))
self.navigationItem.leftBarButtonItem = backBtn
backBtn.tintColor = UIColor.white
}
func backToPreviousVC() {
self.navigationController?.popViewController(animated: true)
}
在viewDidLoad中调用setupBackButton()