我試圖將模板圖像添加到幻燈片菜單控制器(基於此SideMenuController component)。但我有一個色彩問題。原始圖像是深灰色的,我希望它們是白色的。我給自己定.alwaysTemplate
模式和色調的顏色編程,但不是必需的行爲,我得到這個:UIImageView模板圖像色彩在UITableViewCell上的更改單擊
即,只有當我敲擊該菜單項色調的顏色變化從灰色到白色。你知道如何解決這個問題嗎?
這裏是導航菜單控制器的代碼,我有:
class NavigationMenuController: UITableViewController {
@IBOutlet weak var groupsIcon: UIImageView!
@IBOutlet weak var calendarIcon: UIImageView!
@IBOutlet weak var documentsIcon: UIImageView!
@IBOutlet weak var settingsIcon: UIImageView!
private let gradientTop = UIColor.fromHexadecimal(0xF3736F)
private let gradientBottom = UIColor.fromHexadecimal(0xEC92AE)
private let cellSelection = UIColor.fromHexadecimal(0xEC92AE)
private let menuIconTint = UIColor.fromHexadecimal(0xFFFFFF)
private let segues = [
"embedGroupsController",
"embedCalendarController",
"embedRulesController",
"embedSettingsController"
]
override func viewDidLoad() {
super.viewDidLoad()
let gradient = CAGradientLayer()
gradient.colors = [gradientTop.cgColor, gradientBottom.cgColor]
gradient.locations = [0.0, 1.0]
gradient.frame = tableView.bounds
let backgroundView = UIView(frame: tableView.bounds)
backgroundView.layer.insertSublayer(gradient, at: 0)
tableView.backgroundView = backgroundView
let imageViews: [UIImageView] = [groupsIcon, calendarIcon, documentsIcon, settingsIcon]
for imageView in imageViews {
guard let image = imageView.image else { fatalError("Image not found") }
let templateImage = image.withRenderingMode(.alwaysTemplate)
imageView.image = templateImage
imageView.tintColor = menuIconTint
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = super.tableView(tableView, cellForRowAt: indexPath)
let bgColorView = UIView()
bgColorView.backgroundColor = cellSelection
cell.selectionStyle = .default
cell.backgroundColor = .clear
cell.selectedBackgroundView = bgColorView
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let sideMenu = sideMenuController else {
fatalError("Side menu controller is not configured")
}
if indexPath.row >= segues.count {
fatalError("Unexpected row index: \(indexPath.row)")
}
sideMenu.performSegue(withIdentifier: segues[indexPath.row], sender: nil)
}
}
我也試圖通過故事板和資產設置做到這一點,但有同樣的效果。我在iOS 10和Swift 3上運行這個應用程序。
這可能有助於https://stackoverflow.com/questions/6745919/uitableviewcell-subview-disappears-when-cell-is-selected – Aakash
你正在設置圖像上的白色tintcolor,但選擇另一個單元格,你必須重置先前選擇的細胞。所以你必須將tintColor設置爲黑暗灰色以前選擇的單元格 –
@HiteshAgarwal不,我的意思是,我希望讓它們全都是白色的,而不是灰色的。但它只發生在點擊之後。他們都應該無條件白色。 – devforfu