Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom BackButton on NavigationController not appearing

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

screenshot

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.

like image 646
user2695433 Avatar asked Sep 03 '26 09:09

user2695433


2 Answers

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
}
like image 69
Anton Belousov Avatar answered Sep 05 '26 00:09

Anton Belousov


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.

like image 40
Pradeep K Avatar answered Sep 04 '26 23:09

Pradeep K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!