I have several buttons in my app that utilize images that are pictures.
For the buttons set in Interface Builder, I set the image in attributes inspector. It displays without a tint, as I desire. I checked the method used to set the image in interface builder is UIButton setImage:forState:
Now, if I try to do set the image of the button in cellForRowAtIndexPath
in a UITableViewController
, there's a tint over the image.
// This puts a tint over the button
customCell.myButton.setImage(buttonImage, forState: .Normal)
As a workaround, I threw an UIImageView
behind the button, but when I animate the button the imageView doesn't move, which I find annoying.
I've also tried this:
// This results in a blank image on the button
customCell.myButton.imageView?.contentMode = .ScaleAspectFit
customCell.myButton.imageView?.image = buttonImage
If anyone's got suggestions re: what I'm doing wrong, I welcome your input.
It turns out imageWithRenderingMode
solves the problem.
Creates and returns a new image object with the specified rendering mode. A new image object with the specified rendering mode.
I needed to use the AlwaysOriginal
case:
Always draw the original image, without treating it as a template.
let buttonImage = UIImage(named: buttonImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.