tintColor不会在主题中更新为默认值

时间:2018-09-03 05:54:35

标签: ios swift

我有一个自定义图标(.png图像)作为barbutton,并且我将使用Theme.swift文件设置1种特定视图控制器的资产颜色/背景颜色,但是由于某种原因,色泽不是正在更新。

当前是将其添加到我的Assets文件夹时使用的深灰色,但根据我的Theme.swift应该显示为绿色。有谁知道为什么可能无法更新为绿色的默认新颜色的原因:

enter image description here

在我要查找主题的viewcontroller的viewDidLoad中,我已添加

Theme.current.apply()

这是我的Theme.swift文件,具有默认的绿色:

import UIKit

enum Theme: Int {
    //1
    case `default`, dark, graphical

    //2
    private enum Keys {
        static let selectedTheme = "SelectedTheme"
    }

    //3
    static var current: Theme {
        let storedTheme = UserDefaults.standard.integer(forKey: Keys.selectedTheme)
        return Theme(rawValue: storedTheme) ?? .default
    }

    var mainColor: UIColor {
        switch self {
        case .default:
            print("/default")
            return UIColor(red: 87.0/255.0, green: 188.0/255.0, blue: 95.0/255.0, alpha: 1.0)
        case .dark:
            return UIColor(red: 255.0/255.0, green: 115.0/255.0, blue: 50.0/255.0, alpha: 1.0)
        case .graphical:
            return UIColor(red: 10.0/255.0, green: 10.0/255.0, blue: 10.0/255.0, alpha: 1.0)
        }
    }


func apply() {

    //2
    UIApplication.shared.delegate?.window??.tintColor = mainColor
}
}

/ default甚至可以在我的控制台中打印,但是颜色不会更新。

2 个答案:

答案 0 :(得分:0)

在这里,图像renderingMode有问题。如果要在图像上应用tintColor,则必须将图像渲染模式用作alwaysTemplate

可以使用两种方法设置图像渲染模式。

以编程方式

if let myImage = UIImage(named: "myImage") {
    let tintableImage = myImage.withRenderingMode(.alwaysTemplate)
    imageView.image = tintableImage
}

从资产

enter image description here

答案 1 :(得分:0)

尝试更改NavigationBar tint color

更改导航栏色调颜色的示例代码

UINavigationBar.appearance().tintColor = UIColor.purple //your selected color

申请方法的样品请检查

func apply() {

    UIApplication.shared.delegate?.window??.tintColor = mainColor

    // change navigation bar tint colour
    UINavigationBar.appearance().tintColor = mainColor // or your selected color

}