Lolidze
Lolidze

Reputation: 219

Swift hide back button

I need to hide back button and paste another button

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)

but this code didnt work, back button dont hided or replaced to another button

how can i solve this problem ?

show this vc like this

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

Upvotes: 0

Views: 193

Answers (2)

Yogesh Tandel
Yogesh Tandel

Reputation: 1754

Use the below code

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

}

Upvotes: 1

Niilesh R Patel
Niilesh R Patel

Reputation: 707

First, Create BaseViewController in your project and set backButton hide code and add custom back button code in viewDidLoad.

After that, all the controller of your project should inherit from BaseViewController so new back button enables for all controller.

BaseViewContorller

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.hidesBackButton = true 
    self.setBackButton()// Set Custom back button
}

Set Custom BackButton Code

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

Upvotes: 1

Related Questions