添加自定义后退按钮对 UINavigationBar 不起作用
Adding custom back button not working to UINavigationBar
我正在尝试将自定义后退按钮添加到我的导航控制器,但它无法正常工作。
func setupNavBarItems() {
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = UIColor.clear
let backButton = UIButton(type: .system)
backButton.setImage(#imageLiteral(resourceName: "icon_back"), for: .normal)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
}
而且我也尝试过这个但没有任何运气(我用以下代码替换了底部的两行)
let backButton = UIButton(type: .system)
backButton.setImage(#imageLiteral(resourceName: "icon_back"), for: .normal)
let backBarButton = UIBarButtonItem.init(customView: backButton)
navigationItem.setLeftBarButton(backBarButton, animated: true)
我看到了自定义按钮图像,但它不会触发后退按钮操作。
在这种情况下,最好将其声明为实例变量,因为除了目标 popViewController
之外,目标将与局部变量 / lazy
一起丢失
let addButton = UIBarButtonItem(image:UIImage(named:"your_icon_name"), style:.plain, target:self, action:#selector(YourControllerName.buttonAction(_:)))
addButton.tintColor = UIColor.white
self.navigationItem.leftBarButtonItem = addButton
.....
@objc func buttonAction(_ sender: UIBarButtonItem) {
self.navigationController?.popViewController(animated: true)
}
我正在尝试将自定义后退按钮添加到我的导航控制器,但它无法正常工作。
func setupNavBarItems() {
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = UIColor.clear
let backButton = UIButton(type: .system)
backButton.setImage(#imageLiteral(resourceName: "icon_back"), for: .normal)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
}
而且我也尝试过这个但没有任何运气(我用以下代码替换了底部的两行)
let backButton = UIButton(type: .system)
backButton.setImage(#imageLiteral(resourceName: "icon_back"), for: .normal)
let backBarButton = UIBarButtonItem.init(customView: backButton)
navigationItem.setLeftBarButton(backBarButton, animated: true)
我看到了自定义按钮图像,但它不会触发后退按钮操作。
在这种情况下,最好将其声明为实例变量,因为除了目标 popViewController
lazy
一起丢失
let addButton = UIBarButtonItem(image:UIImage(named:"your_icon_name"), style:.plain, target:self, action:#selector(YourControllerName.buttonAction(_:)))
addButton.tintColor = UIColor.white
self.navigationItem.leftBarButtonItem = addButton
.....
@objc func buttonAction(_ sender: UIBarButtonItem) {
self.navigationController?.popViewController(animated: true)
}