0

I'm trying to add an icon (and keep the title) to the back button of my navigation controller. It seems if I set the image of the UIBarButtonItem it hides the title, so I thought I'd try a custom view. I've tried

UIButton* customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
[customButton setTitle:@"Title" forState:UIControlStateNormal];
[customButton setAdjustsImageWhenHighlighted:YES];
[customButton setFrame:CGRectMake(0, 0, 125, 32)];

UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.backBarButtonItem = backButton;

but Apple docs say that the backBarButtonItem ignores custom views, so this doesn't work.

I also tried this:

self.navigationItem.leftBarButtonItem = backButton;
self.navigationItem.hidesBackButton = YES;

but the leftBarButtonItem shows up one screen too soon and the hidesBackButton doesn't seem to hide the backBarButtonItem.

Is there another way to get both an image and title onto a navigation backBarButtonItem?

Trevor
  • 21
  • 5

1 Answers1

1

Very first hide the back button provided by UINavigationController by susing the code self.navigationItem.hidesBackButton = YES;

And set own button with image and title with the following code

UIButton* customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[customButton setImage:[UIImage imageNamed:@"icon"] forState:UIControlStateNormal];
[customButton setTitle:@"Title" forState:UIControlStateNormal];
[customButton setAdjustsImageWhenHighlighted:YES];
[customButton setFrame:CGRectMake(0, 0, 125, 32)];

UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationItem.backBarButtonItem = backButton;
Vinodh
  • 5,262
  • 4
  • 38
  • 68
  • 1
    > iOS7: "When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway" – SoftDesigner Jan 30 '14 at 15:36
  • then how can i add my own image as back button ? @SoftDesigner – Vinodh Jan 31 '14 at 06:42
  • I used this solution http://stackoverflow.com/a/20283490/649379 @Vinodh. Also I'll try hiding back button: http://stackoverflow.com/a/21225089/649379 – SoftDesigner Jan 31 '14 at 14:02