2

I am always setting the backgroundImages of UINavigationBars, but I never worked out how to set the backgroundImage of a the back button of a UINavigationBar.

How could I do this?

Tim Cooper
  • 151,519
  • 37
  • 317
  • 271
max_
  • 23,337
  • 38
  • 120
  • 209

3 Answers3

2
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,36,30);
[button setBackgroundImage:[UIImage imageNamed:@"backgroundImage.png"] forState:UIControlStateNormal];

//[button setTitle:@"  Back" forState:UIControlStateNormal];
//[button.titleLabel setFont:[UIFont boldSystemFontOfSize:11]];

[button addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
Oleh Kudinov
  • 2,453
  • 24
  • 29
1

I think you will have to create a custom button for the backBarButtonItem, and then customize it.

If you are looking for tutorials, then go to this question - How do you change color/image on the default backBarButtonItem? and I think you find your answer there.

For making a bar button item look like a backBarButtonItem, refer to this answer. Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar

Community
  • 1
  • 1
Legolas
  • 12,007
  • 12
  • 76
  • 129
  • problem is that I want to keep the 'arrow' shape of it instead of exchanging it for a rectangle.. – max_ Jun 10 '11 at 22:41
0
UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.frame = CGRectMake(0,0,36,30);
[backbutton setBackgroundImage:[UIImage imageNamed:@"backgroundImage.png"] forState:UIControlStateNormal];

[backbutton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
stema
  • 85,585
  • 19
  • 101
  • 125
Jagat Dave
  • 1,623
  • 3
  • 23
  • 30