IOS 5 changes the background of the back button in the navigation controller to transparent

I set up the title bar of the navigation controller with a background image, but I'm really trying to change the background color of the button back to transparent so that it matches the green title bar below it. I am new to iOS development. Can anyone suggest what can be done?

Here is the code I used to change the title bar of the navigation controller, just in case:

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) { UIImage *image = [UIImage imageNamed:@"greenbar.png"]; [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; // [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; } //change back button image } 

EDIT : after a little research, I managed to get what I wanted. Here is the code to change the background image for the back button:

  UIImage *image1 = [UIImage imageNamed:@"back-bt.png"]; [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image1 forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; 

The code above adds an image to all the buttons back in the navigation controller.

+6
source share
1 answer

You cannot change the appearance of the default back button, but you can create your own button to replace it.

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) { UIImage *image = [UIImage imageNamed:@"greenbar.png"]; [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; // [[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault]; } //change back button image if(self.navigationController.viewControllers.count > 1) { UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; [backButton setTitle:@"Back" forState:UIControlStateNormal]; [backButton addTarget:self action:@selector(didTapBackButton:) forControlEvents:UIControlEventTouchUpInside]; backButton.frame = CGRectMake(0.0f, 0.0f, 64.0f, 41.0f); UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton]; self.navigationItem.leftBarButtonItem = backButtonItem; } } - (void) didTapBackButton:(id)sender { if(self.navigationController.viewControllers.count > 1) { [self.navigationController popViewControllerAnimated:YES]; } } 
+5
source

Source: https://habr.com/ru/post/915177/


All Articles