Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting navigation bar back button image

I want to set the back button in my UINavigationBar to this image:

enter image description here

I don't want the image to be embedded in the standard back button image, I just want this image to appear.

I know from looking at other questions that I can use:

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

to set the background image of the back button. But this causes the image to be stretched, and this particular image is not, as far as I can tell, suitable to be stretched.

Is there a way that I can replace back button image with my image?

I am supporting iOS 5.0 and up.

like image 498
Darren Avatar asked Apr 27 '13 16:04

Darren


People also ask

How do I get rid of the back button on my navigation bar?

Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.

How do I change the navigation bar on my Iphone?

A user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.


3 Answers

Since iOS 7+ you should use backIndicatorImage property of UINavigationBar to set your custom indicator image. Also you need to provide backIndicatorTransitionMaskImage(you may use the same image for both).

Swift:

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

Objective-C:

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back-button-image"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back-button-image"]];
like image 68
Alexander Tkachenko Avatar answered Oct 17 '22 22:10

Alexander Tkachenko


From the docs for UIBarButtonItem setBackButtonBackgroundImage:forState:barMetrics::

For good results, backgroundImage must be a stretchable image.

So, make it stretchable. I.e. specify which parts of the image can be stretched, and more importantly, which parts can not be stretched. In your case this will be the edges of the image (the part not containing the arrow).

UIImage resizableImageWithCapInsets:

The alternative is to supply a number of images (one for each bar metric) which is of a size that means it won't need to be scaled. raywenderlich user-interface-customization. But you're still going to want to make the image stretchable so you have control over what happens.

If you can't find a stretch spec which works, your fallback position is to create a template back button item for each instance of each view controller and set it as the backBarButtonItem for its navigation item.

like image 40
Wain Avatar answered Oct 17 '22 22:10

Wain


swift version :-

    self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "HomeLeft@2x")
    self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "HomeLeft@2x")
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

put this in viewDidLoad()

like image 35
Chathuranga Silva Avatar answered Oct 18 '22 00:10

Chathuranga Silva