Custom UIBarButtonItem with image and tintColor

202 views Asked by At

My navigation bar has three components: a back button, the tile and and additional custom button.

My goal is to be able to add a custom image to that button but still obey the tint colour logic.

In my AppDelegate.m didFinishLaunchingWithOptions method I have set:

ViewController *welcomeVC = [[ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc]
                                                initWithRootViewController:welcomeVC];
navigationController.navigationBar.tintColor = [UIColor orangeColor];
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor], NSForegroundColorAttributeName, nil]];

self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];

Then in ViewController.m I have:

- (void)viewWillAppear:(BOOL)animated
{
    [self setUpImageBackButton];
}

- (void)setUpImageBackButton {
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(popCurrentViewController)];
}

-(IBAction)nextButtonTapped:(id)sender {
//create the second view
    SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:nil bundle:NULL];
    secondController.title = @"SecondViewController";
    [self.navigationController pushViewController:secondController animated:YES];
}

-(void)popCurrentViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

And in the last view controller I have:

- (void)viewWillAppear:(BOOL)animated
{
    [self setUpImageBackButton];
}

- (void)setUpImageBackButton {
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(popCurrentViewController)];

    UIButton *settingsButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [settingsButton setTitle:@"\u2699" forState:(UIControlStateNormal)];
    [settingsButton.titleLabel setFont:[UIFont systemFontOfSize:30]];
    [settingsButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:settingsButton];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor orangeColor];
}

-(IBAction)nextButtonTapped:(id)sender {
    //create the third view
    ThirdViewController *thirdController = [[ThirdViewController alloc] initWithNibName:nil bundle:NULL];
    thirdController.title = @"ThirdViewController";
    [self.navigationController pushViewController:thirdController animated:YES];
}

-(void)popCurrentViewController {
    [self.navigationController popViewControllerAnimated:YES];
}

My questions are:

  1. Can I use custom images for the navigation bar buttons but still have the tintColor effect? If so, can someone post a code sample how to do that.
  2. Why does my settings button not obey the tint colour rule, as it stay white. The info button and the back button are all orange, as I set them in the AppDelegate call.
0

There are 0 answers