Search code examples
objective-ccocoa-touchuinavigationcontrolleruinavigationbaruinavigationitem

How do I correctly set the tint colour of a UINavigationBar in iOS7?


I'm using the last two lines of the code below to change the tint colour of my navigation bar so that my UIBarButtonItems display as black instead of the default blue. This code works in another controller but not in this controller.

When I navigate forward to that controller then navigate back to the controller in question then the UIBarButtonItems are black as needed. However when I first load the app and this view is loaded they are blue.

How do I correctly change the tint colour of my navigation bar so my UIBarButtonItem's display as black?

The actual images I've set for the buttons are black.

Code in viewWillAppear:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // Logo button
    UIButton *logoButton = [[UIButton alloc] init ];
    [logoButton setImage:[UIImage imageNamed:@"va_logohme.png"] forState:UIControlStateNormal];
    [logoButton setAdjustsImageWhenHighlighted:NO];                  ;
    [logoButton setFrame:CGRectMake(0, 0, 320, 40)];
    [logoButton addTarget:self action:@selector(logoButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
    [[self navigationItem] setTitleView:logoButton];

    // Additional UIBarButtonItem's
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"hamburger_for_more.png"] style:UIBarButtonItemStylePlain target:self action:@selector(menuButtonTap)];

    UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"magnify_glass.png"] style:UIBarButtonItemStylePlain target:self action:@selector(searchButtonTapped)];
    [[self navigationItem] setLeftBarButtonItems:@[menuButton, searchButton]];
    UIBarButtonItem *shoppingCartButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shopping_bag.png"] style:UIBarButtonItemStylePlain target:self action:@selector(shoppingCartButtonTapped)];
    UIBarButtonItem *addFavouriteButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_fav_heart.png"] style:UIBarButtonItemStylePlain target:self action:@selector(favouritesPageButtonTapped:)];

    NSMutableArray *buttons = [[NSMutableArray alloc] init];
    for (UIControl *btn in self.navigationController.navigationBar.subviews) {
        if ([btn isKindOfClass:[UIControl class]]) {
            [buttons addObject:btn];
        }
    }

    // Basket container
    _bagContainer = [[UIView alloc] initWithFrame:CGRectMake(277, 16, 21, 21)];
    [[[self navigationController] navigationBar] addSubview:_bagContainer];

    UILabel *bagCount = [[UILabel alloc] init];
    [bagCount setText: [NSString stringWithFormat:@"%i", [Bag totalItems:[self managedObjectContext]]]];
    [bagCount setFont:[UIFont systemFontOfSize:14]];
    [bagCount sizeToFit];

    [_bagContainer addSubview:bagCount];

    bagCount.center = [_bagContainer convertPoint:_bagContainer.center fromView:_bagContainer.superview];
    [_bagContainer setUserInteractionEnabled:NO];

    [[self navigationItem] setRightBarButtonItems:@[shoppingCartButton, addFavouriteButton] animated: YES];

    // Change colour of nav bar tint
    [[[self navigationController] navigationBar] setBarTintColor:[UIColor whiteColor]];
    [[[self navigationController] navigationBar] setTranslucent:NO];
}

Solution

  • If the images are template images, then set their tintColor to the desired color. If they are normal images, then make sure to render them as normal images so that they show up in their own color and not as tinted template images.