thumbtackthief
thumbtackthief

Reputation: 6211

Change the color of an icon within a button within a UIBarButton (Swift)

I have a UIToolbar at the with a UIBarButton containing a button containing an image (the image changes when clicked, so I apparently had to do it that way according to my research).

Although the image itself is white and is a png, it's displaying as black (I want it white). I've set the tint color on the Bar Button Item to white, the text color on the Button to white, and included button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal) in my ViewDidLoad. It's still black. It should always be white, so I don't care whether I do this programmatically or through the InterfaceBuilder.

Upvotes: 0

Views: 2104

Answers (1)

Vitalii Gozhenko
Vitalii Gozhenko

Reputation: 9354

You need to load image in UIImageRenderingModeAlwaysTemplate render mode:

Objective-C

UIImage *toolbarButtonImage = [[UIImage imageNamed:@"SomeImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithImage:toolbarButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(someAction)];

Swift:

let buttonImage = UIImage(named: "SomeImage")!.imageWithRenderingMode(.AlwaysTemplate)
let button = UIBarButtonItem(image: buttonImage, style: .Plain, target: self, action: "someAction:")

Upvotes: 6

Related Questions