1
votes

I'm trying to change the back button to a custom one. If I create a UIBarButtonItem with just a custom title, that works fine. However if I create one with a custom view, the button doesn't show, rather the default back button shows. Any ideas ?

UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
backBtn.frame = CGRectMake(0, 0, 51, 31);
[backBtn setImage:[UIImage imageNamed:@"nav_btn_back.png"] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[[UIBarButtonItem alloc] initWithCustomView:backBtn] autorelease];
self.navigationItem.backBarButtonItem = back;   

2

2 Answers

0
votes

Not sure why that's not working. However I realized that I can just set this custom button as a leftBarButtonItem for my view

0
votes

You can use setBackButtonBackgroundImage (Apple doc)

Usage:

let backImg: UIImage = UIImage(named: "img_name")!
backButton.setBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)

If you are using it in all controllers, use appearance:

let backImg: UIImage = UIImage(named: "img_name")!
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)