快速隐藏后退按钮

时间:2019-02-26 11:20:08

标签: swift

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

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)

2 个答案:

答案 0 :(得分:1)

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

之后,您项目的所有控制器都应继承自BaseViewController,以便为所有控制器启用新的后退按钮。

BaseViewContorller

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)
}

答案 1 :(得分:1)

使用以下代码

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!) {

}