Question

ΩI've been trying to do this for like an hour now, nothing seems to work:

Things I've tried:

1) Doesn't work.

UIImage *image = [UIImage imageNamed:@"scr1_button_topbar.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
[v addSubview:button];
UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithCustomView:v];
self.navigationItem.backBarButtonItem= forward;

2) Works, but looks REALLY stupid, it leaves the "Back" text there.

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"scr2_btnback"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

3) Button doesn't show

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"scr2_btnback"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

And setting the text to @""

4) Setting the left bar button item - Works, but that's a little cheating, I want the back button, not the left button.

UIImage *backImage = [UIImage imageNamed:@"scr2_btnback.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);

[backButton setImage:backImage forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backButton:)    forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton] ;

self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = backBarButtonItem;
Was it helpful?

Solution

Just got told, you can't edit the view of the backBarButtonItem... YEEY me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top