Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView template image tint color changes on UITableViewCell click

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:

enter image description here

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.

like image 668
devforfu Avatar asked Jun 14 '17 10:06

devforfu


2 Answers

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.

like image 67
Rivera Avatar answered Oct 22 '22 13:10

Rivera


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
}
like image 38
devforfu Avatar answered Oct 22 '22 13:10

devforfu