2

I am using the following code for a custom back button with title in the nav bar. However, the title is not being shown, any help ?

nb: the back button image is black and the same one thats used by default by ios, needed to customize the action handler, thats why did it this way.

backButton=[UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateSelected];    
[backButton addTarget:self action:@selector(customBack) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0,0,64,32)];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[backButton setTitle:@"Encameo" forState:UIControlStateNormal];
[backButton setTitle:@"Encameo" forState:UIControlStateSelected];

self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView: backButton];
Ahsan
  • 2,964
  • 11
  • 53
  • 96
  • Check this out. http://stackoverflow.com/questions/2681321/uibarbuttonitem-with-custom-image-and-no-border/2681430#2681430 – Trevor Jul 20 '11 at 16:41

1 Answers1

10

You are setting image on button instead of this you need to set backgroundImage of UIButton then your title will be displayed.

Change the code like this

backButton=[UIButton buttonWithType:UIButtonTypeCustom];
[backButton setBackgroundImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateNormal];
[backButton setBackgroundImage:[UIImage imageNamed:@"backbutton.png"] forState:UIControlStateSelected];    
[backButton addTarget:self action:@selector(customBack) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0,0,64,32)];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[backButton setTitle:@"Encameo" forState:UIControlStateNormal];
[backButton setTitle:@"Encameo" forState:UIControlStateSelected];

self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView: backButton];
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
  • If you are using the same title color and title for both normal and selected states, it's enough if you declare for normal – Dejell Apr 08 '13 at 10:44