Conor Conor - 8 months ago 226
Swift Question

Swift Custom NavBar Back Button Image and Text

I need to customise the look of a back button in a Swift project.

Here's what I have:
Default Back Button

Here's what I want:
Custom Back Button

I've tried creating my own UIBarButtonItem but I can't figure out how to get the image to be beside the text, rather than as a background or a replacement for the text.

let backButton = UIBarButtonItem(title: "Custom", style: .Plain, target: self, action: nil )
//backButton.image = UIImage(named: "imageName") //Replaces title
backButton.setBackgroundImage(UIImage(named: "imageName"), forState: .Normal, barMetrics: .Default) // Stretches image
navigationItem.setLeftBarButtonItem(backButton, animated: false)


You can do something like that :

let yourBackImage = UIImage(named: "back_button_image")
self.navigationController?.navigationBar.backIndicatorImage = yourBackImage
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = yourBackImage
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Custom", style: .Plain, target: nil, action: nil)

The only thing is that your image will have a unique color