Swift 快速隐藏后退按钮

Swift 快速隐藏后退按钮,swift,Swift,我需要隐藏后退按钮并粘贴另一个按钮 self.navigationItem.setHidesBackButton(true, animated: false) self.navigationItem.hidesBackButton = true self.navigationController?.navigationItem.hidesBackButton = true self.navigationController?.navigationBar.topItem?.hidesBackBut

我需要隐藏后退按钮并粘贴另一个按钮

self.navigationItem.setHidesBackButton(true, animated: false)
self.navigationItem.hidesBackButton = true
self.navigationController?.navigationItem.hidesBackButton = true
self.navigationController?.navigationBar.topItem?.hidesBackButton = true

self.tabBarController?.navigationController?.navigationItem.hidesBackButton = true
self.tabBarController?.navigationController?.navigationItem.setHidesBackButton(true, animated: false)
self.tabBarController?.navigationItem.hidesBackButton = true

let backButton1 = UIBarButtonItem (title: "Button", style: .plain, target: self, action: #selector(GoToBack))
self.navigationItem.leftBarButtonItem = backButton1
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(GoToBack))

self.tabBarController?.navigationItem.setLeftBarButtonItems([backButton1], animated: true)
但此代码不起作用,后退按钮不隐藏或替换为其他按钮

self.navigationItem.setHidesBackButton(true, animated: false)
self.navigationItem.hidesBackButton = true
self.navigationController?.navigationItem.hidesBackButton = true
self.navigationController?.navigationBar.topItem?.hidesBackButton = true

self.tabBarController?.navigationController?.navigationItem.hidesBackButton = true
self.tabBarController?.navigationController?.navigationItem.setHidesBackButton(true, animated: false)
self.tabBarController?.navigationItem.hidesBackButton = true

let backButton1 = UIBarButtonItem (title: "Button", style: .plain, target: self, action: #selector(GoToBack))
self.navigationItem.leftBarButtonItem = backButton1
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(GoToBack))

self.tabBarController?.navigationItem.setLeftBarButtonItems([backButton1], animated: true)
我怎样才能解决这个问题

像这样显示这个
vc

self.navigationController?.pushViewController(viewcontroller, animated: true)

首先,在项目中创建
BaseViewController
,设置backButton隐藏代码,并在
viewDidLoad
中添加自定义后退按钮代码

之后,项目的所有控制器都应该继承自
BaseViewController
,这样所有控制器都可以使用newback按钮

BaseViewController

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.hidesBackButton = true 
    self.setBackButton()// Set Custom back button
}
设置自定义BackButton代码

//Add Custom Back Button
fileprivate func setBackButton() {
    let button   = UIButton(type: UIButton.ButtonType.custom) as UIButton
    button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
    button.contentHorizontalAlignment = .fill
    button.contentVerticalAlignment = .fill
    button.imageView?.contentMode = .center
    button.contentEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)

    button.setImage(UIImage(named: "backButton.png"), for: .normal)
    button.addTarget(self, action: #selector(btnBackActionHandler(_:)), for:.touchUpInside)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)

}


@objc func btnBackActionHandler(_ sender : AnyObject) {
    self.navigationController?.popViewController(animated: true)
}

首先,在项目中创建
BaseViewController
,设置backButton隐藏代码,并在
viewDidLoad
中添加自定义后退按钮代码

之后,项目的所有控制器都应该继承自
BaseViewController
,这样所有控制器都可以使用newback按钮

BaseViewController

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.hidesBackButton = true 
    self.setBackButton()// Set Custom back button
}
设置自定义BackButton代码

//Add Custom Back Button
fileprivate func setBackButton() {
    let button   = UIButton(type: UIButton.ButtonType.custom) as UIButton
    button.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
    button.contentHorizontalAlignment = .fill
    button.contentVerticalAlignment = .fill
    button.imageView?.contentMode = .center
    button.contentEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 0)

    button.setImage(UIImage(named: "backButton.png"), for: .normal)
    button.addTarget(self, action: #selector(btnBackActionHandler(_:)), for:.touchUpInside)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)

}


@objc func btnBackActionHandler(_ sender : AnyObject) {
    self.navigationController?.popViewController(animated: true)
}
使用下面的代码

self.navigationItem.setHidesBackButton(true, animated:true);
addNavButtons()

//MARK:- NAVIGATION BUTTONS
func addNavButtons(){

    let btn_back = UIButton(type: .custom)
    btn_back.setImage(UIImage(named: "icon_back"), for: .normal)
    btn_back.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    btn_back.addTarget(self, action: #selector(goBack), for: .touchUpInside)
    btn_back.imageView?.contentMode = UIViewContentMode.scaleAspectFit
    let menuitem1 = UIBarButtonItem(customView: btn_back)
    self.navigationItem.setLeftBarButtonItems([menuitem1], animated: true)

    let btn_search = UIButton(type: .custom)
    btn_search.setImage(UIImage(named: "searchby_icon"), for: .normal)
    btn_search.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    btn_search.addTarget(self, action: #selector(SearchButtonClick), for: .touchUpInside)
    btn_search.imageView?.contentMode = UIViewContentMode.scaleAspectFit
    let menuitem2 = UIBarButtonItem(customView: btn_search)

    self.navigationItem.setRightBarButtonItems([menuitem2], animated: true)
}

@objc func goBack(sender:UIButton!) {
     _ = navigationController?.popViewController(animated: true)
}

@objc func SearchButtonClick(sender:UIButton!) {

}
使用下面的代码

self.navigationItem.setHidesBackButton(true, animated:true);
addNavButtons()

//MARK:- NAVIGATION BUTTONS
func addNavButtons(){

    let btn_back = UIButton(type: .custom)
    btn_back.setImage(UIImage(named: "icon_back"), for: .normal)
    btn_back.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    btn_back.addTarget(self, action: #selector(goBack), for: .touchUpInside)
    btn_back.imageView?.contentMode = UIViewContentMode.scaleAspectFit
    let menuitem1 = UIBarButtonItem(customView: btn_back)
    self.navigationItem.setLeftBarButtonItems([menuitem1], animated: true)

    let btn_search = UIButton(type: .custom)
    btn_search.setImage(UIImage(named: "searchby_icon"), for: .normal)
    btn_search.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    btn_search.addTarget(self, action: #selector(SearchButtonClick), for: .touchUpInside)
    btn_search.imageView?.contentMode = UIViewContentMode.scaleAspectFit
    let menuitem2 = UIBarButtonItem(customView: btn_search)

    self.navigationItem.setRightBarButtonItems([menuitem2], animated: true)
}

@objc func goBack(sender:UIButton!) {
     _ = navigationController?.popViewController(animated: true)
}

@objc func SearchButtonClick(sender:UIButton!) {

}

您必须更换
viewController
中的后退按钮。尝试使用
viewController.navigationItem.leftBarButtonim
您必须替换
viewController
中的后退按钮。尝试使用
viewController.navigationItem.leftBarButtonim
请不要为一个小补丁推送可能对OP有效或无效的整个设计模式。请为一个小补丁推送可能对OP有效或无效的整个设计模式。