Setting navigation bar back button image Setting navigation bar back button image ios ios

Setting navigation bar back button image


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"]];


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.


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()