Using this method to change title and background image of the back navigation button. Title is changed but button stays black
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"topBarbackground.png"] forBarMetrics:UIBarMetricsDefault];
UIBarButtonItem *backButton = [UIBarButtonItem new];
[backButton setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[backButton setTitle:NSLocalizedString(@"Back", nil)];
[[self navigationItem] setBackBarButtonItem:backButton];
}
What is a proper way of doing this?
Use either setImage
or setBackButtonBackgroundImage
because setBackgroundImage
has no effect:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"topBarbackground.png"] forBarMetrics:UIBarMetricsDefault];
UIBarButtonItem *backButton = [UIBarButtonItem new];
// Put the image inside the button
[backButton setImage:[UIImage imageNamed:@"backButton.png"];
[backButton setTitle:NSLocalizedString(@"Back", nil)];
[[self navigationItem] setBackBarButtonItem:backButton];
}
or
{
[super viewDidLoad];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"topBarbackground.png"] forBarMetrics:UIBarMetricsDefault];
UIBarButtonItem *backButton = [UIBarButtonItem new];
// Use the image as the button (like custom button)
[backButton setBackButtonBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[backButton setTitle:NSLocalizedString(@"Back", nil)];
[[self navigationItem] setBackBarButtonItem:backButton];
}
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:@selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchUpInside];
backButton.frame = kBarBackButtonFrame;
[backButton setExclusiveTouch:YES];
[backButton setBackgroundImage:[UIImage imageNamed:NAV_BAR_BACK_DEFAULT_BUTTON] forState:UIControlStateNormal];
[backButton setBackgroundImage:[UIImage imageNamed:NAV_BAR_BACK_DEFAULT_BUTTON] forState:UIControlStateHighlighted];
[backButton setBackgroundImage:[UIImage imageNamed:NAV_BAR_BACK_DEFAULT_BUTTON] forState:UIControlStateSelected];
[backButton setTitle:NSLocalizedString(@"Back", nil) forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont systemFontOfSize:kBarBackButtonTitleFontSize];
[backButton setTitleColor:kBackButtonTitleColor forState:UIControlStateNormal];
UIBarButtonItem *backButtonHolder = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.rightBarButtonItem = backButtonHolder;
Keep in mind that every UIViewController in the stack has its own navigationItem, so the best solution would be to make a UIViewController subclass, and every UIViewController you use, should extend the previous class. I'm sure there are better solutions, but this is the one that I use.
- (void)popViewControllerWithAnimation {
[self.navigationController popViewControllerAnimated:YES];
}