2017-02-28 76 views
0

這是項目在選擇時的外觀。當行被選中時更改UIImageView中的圖像?

enter image description here

常行有黑色的背景,圖標有白色的前景。但選擇一行將使背景變成半白色。是否可以將圖像更改爲相反的顏色,或者在選擇imageView的行時使用imageView中的不同圖像?

回答

0

使用@ NSDmitry的例子,你需要重寫的UITableViewCellsetSelected(_ selected: Bool, animated: Bool)並設置全contentViewtintColor或只是imageView

0

已經有這個問題的答案Changing UIImage color

你應該聲明你的形象作爲掩膜(.alwaysTemplate),那麼你可以爲任何UIColor改變tintColor

0

是的,色彩的UIImageView可以設置,使用突出顯示的圖像。它有兩種類型的圖像屬性。正常和突出顯示的圖像。
此選項在接口文件(storyboard/XIB)中的屬性檢查器中可用。
另一個選項可用於矢量圖像,您可以在其中爲圖像資源的模板類型設置tintColor。

Swift 3 您也可以以編程方式設置兩個屬性。

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

根據您的tableview行選擇狀態,將圖像的狀態從正常狀態更改爲突出顯示,反之亦然。 imageView.isHighlighted =真/假

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

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


enter image description here