I'm trying to add template images to the slide menu controller (based on this SideMenuController component). But I have an issue with tint color. Original images are dark grey, and I want them to be white. I've set .alwaysTemplate
mode and tint colors programmatically, but instead of required behaviour I am getting this:
I.e., tint color changes from grey to white only when I am tapping that menu item. Do you know how to fix that?
Here is a code I have for navigation menu controller:
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)
}
}
I also tried to do it via Storyboard and Assets settings, but having same effect. I am running this app on iOS 10 and Swift 3.
I had the same issues with a UIImageView
tint inside a cell.
I fixed it by setting the image view tint to Default and setting the cell's content view tint instead.
Ok, yes, @Aakash and @HiteshAgarwal are right, here is a solution to my problem. I set tint color in cellForRowAt
delegate method and it changed to required color (though I still do not know why Storyboard approach failed and why manual color setting is required):
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
if let imageView = getCellImageView(cell) {
imageView.tintColor = UIColor.white
}
return cell
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With