如何根据条件更改应用的颜色主题?

时间:2018-11-20 12:44:54

标签: ios swift

我正在尝试找到有关更改应用程序颜色主题的最佳方法。经过调查,我启发了Durul Dalkanat在https://medium.com/@duruldalkanat/part-2-installing-theme-manager-18a32c314cf1上的帖子,并编写了以下代码以应用于我的应用。

我的问题是用户切换开关按钮时颜色主题不会改变。

如有必要进行详细说明,

我创建了一个包含我的颜色的枚举。

enum ColorTheme: Int {
case day, night

var mainColor:UIColor {
    switch self {
    case .day:
        return UIColor().hexToUIColor("D9D8C8")
    case .night:
        return UIColor().hexToUIColor("141B1B")

    }
}}

然后,我创建了一个结构,以便将我的颜色主题应用于应用程序中的所有组件,并设置为用户默认值,然后从那里获取内容,如下所示。

let selectedThemeKey = "SelectedTheme"

struct ThemeManager {

static func currentTheme() -> ColorTheme {

    if let storedTheme = (UserDefaults.standard.value(forKey: selectedThemeKey) as AnyObject).integerValue {
        return ColorTheme(rawValue: storedTheme)!
    } else {
        return .night
    }
}

static func applyTheme(theme:ColorTheme){

    UserDefaults.standard.set(theme.rawValue, forKey: selectedThemeKey)
    UserDefaults.standard.synchronize()

    UITabBar.appearance().backgroundColor = currentTheme().mainColor
}}

然后我将代码添加到 AppDelegate中的didFinishLaunchWithOptions方法

let defaultTheme = ThemeManager.currentTheme()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    let defaultTheme = ThemeManager.currentTheme()
    ThemeManager.applyTheme(theme: defaultTheme)
    return true

}

最后,我在我的SettingViewController中添加了一个UISwitch来更改我的颜色主题,并且我希望通过以下操作来管理我的颜色主题。

let appDelegate = UIApplication.shared.delegate as! AppDelegate

@IBAction func colorModeSwitchSwiped(_ sender: UISwitch) {


    if colorModeSwitch.isOn {
        ThemeManager.applyTheme(theme: ColorTheme.day)

    }else {
        ThemeManager.applyTheme(theme: ColorTheme.night)

    }

}

此代码可在ViewController中在我切换时更改视图背景颜色,但是必须重新启动应用程序才能更改UITabBar颜色。颜色不会像我在ViewControllers中定义的那样同时改变。 ViewController示例;

override func viewWillAppear(_ animated: Bool) {
    self.view.backgroundColor = ThemeManager.currentTheme().mainColor
}

如何解决此问题?正确的方法是什么?

2 个答案:

答案 0 :(得分:1)

看看UIAppearance的{​​{3}},有一个注释说:

  

iOS会在视图进入窗口时应用外观更改,但不会   更改窗口中已有视图的外观。改变   当前在窗口中的视图的外观,请删除该视图   然后从视图层次结构中放回去。

因此,如果通过外观代理进行设置,则选项卡栏颜色将不会更改,因为它已经在视图层次结构中。您需要直接设置它:

tabBar.tintColor = ThemeManager.currentTheme().mainColor

答案 1 :(得分:0)

没有UITabBar.appearance()的情况下请勿使用didFinishLaunchingWithOptions。 您检查应用程序是否具有tabBarController,然后更改此tabBarController.tabBar颜色。

static func applyTheme(theme:ColorTheme){

    UserDefaults.standard.set(theme.rawValue, forKey: selectedThemeKey)
    UserDefaults.standard.synchronize()
    guard let tabBar = tabBarController?.tabBar else { return }
    tabBar.barTintColor = currentTheme().mainColor
}
相关问题