smeshko February 2016

Image color in UIButton gets masked to background color

I have a UIButton, which should have a white image and a blue background. The problem is, that the color of the image gets masked by the background color and becomes white blue. This is how it should look like and this is what I get (ignore the difference in the background color). Here's the code where I set the image:

- (UIButton *)navButton
{
    if (_navButton == nil) {

        UIButton *navButton = [UIButton buttonWithType:UIButtonTypeCustom];
        navButton.translatesAutoresizingMaskIntoConstraints = NO;

        [navButton setBackgroundColor:[UIColor appBlue]];
        [navButton setImage: [UIImage imageNamed:@"car_icon"] forState:UIControlStateNormal];
        [navButton setEnabled:NO];

        _navButton = navButton;
    }
    return _navButton;
}

Answers


Reshmi Majumder February 2016

You can use this

      UIImage *image = [[UIImage imageNamed:@"car_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
     [button setImage:image forState:UIControlStateNormal]; 


iPR February 2016

Just set Background image instead of image of button

[navButton setBackgroundColor:[UIColor appBlue]];
[navButton setBackgroundImage: [UIImage imageNamed:@"car_icon"] forState:UIControlStateNormal];
[navButton setEnabled:NO];

Hope this will help you to solve this problem.

Post Status

Asked in February 2016
Viewed 1,532 times
Voted 8
Answered 2 times

Search




Leave an answer