asheyla asheyla - 1 year ago 875
Swift Question

Swift custom back button in navigation bar

I am trying to use a custom image for my back button in the navigation bar. I am using the below code, which adds the image, but also keeps the text "Back" in the button. I want to also remove the text. Can I do that?

self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "icon-back")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "icon-back")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

Answer Source

Try code below :-)

func SetBackBarButtonCustom()
     //Initialising "back button"
     let btnLeftMenu: UIButton = UIButton()
     btnLeftMenu.setImage(UIImage(named: "back_arrow"), forState: UIControlState.Normal)
     btnLeftMenu.addTarget(self, action: "onClickBack", forControlEvents: UIControlEvents.TouchUpInside)
     btnLeftMenu.frame = CGRectMake(0, 0, 33/2, 27/2)
     let barButton = UIBarButtonItem(customView: btnLeftMenu)
     self.navigationItem.leftBarButtonItem = barButton

func onClickBack()