I'm trying to use a custom back button in UINavigationController but the image is not appearing.

Here's the code:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
self.navigationController!.view.backgroundColor = UIColor.clearColor()
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "Back-50")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "Back-50")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
I'm not sure what's going wrong with this code.
Maybe, you should use UIAppearance.
Like this
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UINavigationBar appearance] setBackIndicatorImage: [UIImage imageNamed:@"icon-back"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage: [UIImage imageNamed:@"icon-back"]];
return YES;
}
In swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "")
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "")
return true
}
If you want just your image to appear as the back button then set the tintColor of the navigationBar to clear color and then set an image based UIBarButtonItem as the backBarButtonItem of navigationItem of the controller.
let backButton = UIBarButtonItem(image: UIImage(named: "back"), style: .Plain, target: self, action: "back")
self.navigationController?.navigationBar.tintColor = UIColor.clearColor()
self.navigationItem.backBarButtonItem = backButton
This will make the default back button image (<) not appear (infact it appears but its in clear color so not visible) and just show the back image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With