Home > Software engineering >  How to create custom back button in navigationBar in swift?
How to create custom back button in navigationBar in swift?

Time:08-09

public override func viewDidLoad() {
    super.viewDidLoad()
    navigationItem.backBarButtonItem = .init(image: "back_icon".image, style: .plain, target: nil, action: nil)
}

For this above code, I got my back_icon image with the default back image

Output Image

Any idea to remove the default back button at the same time preserving swipe edge to pop viewController.

CodePudding user response:

You can remove default back Button by following way:

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

And manually add navigation items in left side using navigationitem.leftBarButtonItem.

Hope it helps!

CodePudding user response:

just add left bar button item

navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back-icon"), style: .plain, target: self, action: #selector(backTapped))

and action handle with

@objc
func backTapped() {
    navigationController?.popViewController(animated: true)
}

CodePudding user response:

You should be able to change the back button image globally by using

UINavigationBar.appearance().backIndicatorImage = UIImage(named: "arrow-back")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "arrow-back")

And if you only want to change it in some places:

navigationController?.navigationBar.backIndicatorImage = UIImage(named: "arrow-back")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "arrow-back")  

Removing an arrow at all would be as simple as

navigationController?.navigationBar.backIndicatorImage = UIImage()
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage()
  • Related