Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) 
like image 623
Conor Avatar asked May 31 '16 18:05

Conor


People also ask

How do I add a back button to my storyboard?

Storyboard. You can also set this in the Storyboard. Select UINavigationBar and select the Attributes Inspector tab. Then you can change those two images under Back and Back Mask attributes.


1 Answers

You can do something like that:

let yourBackImage = UIImage(named: "back_button_image") self.navigationController?.navigationBar.backIndicatorImage = yourBackImage self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = yourBackImage self.navigationController?.navigationBar.backItem?.title = "Custom" 

Your image will only have one color though

like image 50
Randy Avatar answered Sep 28 '22 23:09

Randy