简体   繁体   中英

Change image in UIImageView when row is selected?

This is how item looks when it is selected.

在此处输入图片说明

Normally row has black background, and icon has white foreground. But selecting a row will made background semi white. Is it a way to change image to opposite color, or use a different image in imageView when imageView 's row is selected?

There is already answer to this question Changing UIImage color

You should declare your image as the mask( .alwaysTemplate ) then you can change tintColor for any UIColor .

使用@ NSDmitry的例子,你需要重写setSelected(_ selected: Bool, animated: Bool)UITableViewCell并设置tintColor整个的contentView或只是imageView

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 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
}


在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM