Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UIImageView color like UILabel's highlighted property when row is selected?

The highlighted property of UILabel changes the label's text color when a UITableViewCell is selected. Is it possible to do something similar for a UIImageView?

"Highlighted" property for a label

like image 370
János Avatar asked Oct 24 '25 18:10

János


1 Answers

Yes, UIImageView color can be set, using highlighted image. It has two types of properties to images. Normal and Highlighted image.
This options are available in attribute inspector in Interface file (storyboard/XIB).
Another option is available for vector images, where you can set tintColor for template types of image asset.

Swift 3 You can also set both properties programatically.

let imageView = UIImageView(<set your initialiser>)
imageView.image = //Your normal image
imageView.highlightedImage = //You highligted image

Upon your tableview row selection status, change state of image from normal to highlighted and vice-versa. imageView.isHighlighted = true/false

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
   imageView.isHighlighted = true
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    imageView.isHighlighted = false
}


enter image description here

like image 109
Krunal Avatar answered Oct 26 '25 07:10

Krunal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!