Search code examples
iphoneuinavigationcontrolleruiappearance

iPhone - set app wide nav bar back button without text


I've set an image to be the background image for all nav bar back buttons in my app using the appearance property. I do it with the following code in the applicationDidLoad method.

UIImage *backButton = [UIImage imageNamed:@"btn-nav-back"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButton forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

My problem is that whenever the back button appears the image is stretched due to the name of the previous view being set as the back button's title. Is there a system wide way I can stop the title being set? Also attempting to set the back button's title to blank in the actual view controllers has no effect :(

[self.navigationItem.backBarButtonItem setTitle:@""];

Solution

  • UIBarButtonItem *customItem = [[[UIBarButtonItem alloc] initWithCustomView:plainButton] autorelease];
    self.navigationItem.leftBarButtonItem = customItem; 
    [(UIButton*)self.navigationItem.leftBarButtonItem.customView addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    

    Make a custom barButtonItem and exchange it for the default back button.