简体   繁体   English

在UITableViewCell上单击UIImageView模板图像色调变化

[英]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 ). 我正在尝试将模板图像添加到幻灯片菜单控制器(基于此SideMenuController组件 )。 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: 我已经以编程方式设置了.alwaysTemplate模式和着色颜色,但是我得到的是:

在此输入图像描述

Ie, 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. 我也尝试通过Storyboard和Assets设置来完成它,但效果相同。 I am running this app on iOS 10 and Swift 3. 我在iOS 10和Swift 3上运行这个应用程序。

I had the same issues with a UIImageView tint inside a cell. 我在单元格内有一个UIImageView色调的问题。

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. 好的,是的,@ Aakash和@HiteshAgarwal是对的,这是我问题的解决方案。 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): 我在cellForRowAt委托方法中设置了色调颜色并将其更改为所需的颜色(虽然我仍然不知道为什么Storyboard方法失败以及为什么需要手动颜色设置):

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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