Search code examples
ios7uinavigationitem

How do I change the leftbarbuttonitem color in ios7.0


    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

I have done above,but it doesn't work.The color is still as same as the background color!


Solution

  • Create button and substitute

       UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.leftBarButton];
        self.navigationItem.leftBarButtonItem = leftBarButton;
    

    or

    + (UIBarButtonItem*)itemWithNormalImage:(UIImage*)normalImage
                               pressedImage:(UIImage*)pressedImage
                                     target:(id)target
                                     action:(SEL)action
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:normalImage
                forState:UIControlStateNormal];
        [button setImage:pressedImage
                forState:UIControlStateSelected];
        [button setImage:pressedImage
                forState:UIControlStateHighlighted];
    
        button.frame = CGRectMake(0, 0, normalImage.size.width, normalImage.size.height);
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem* item=[[UIBarButtonItem alloc] initWithCustomView:button];
        return item;
    }
    

    Text color:

    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStyleBordered target:self action:nil];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    self.navigationItem.leftBarButtonItems = @[item];