ios - 选择深色模式后如何重新加载 View ?

标签 ios swift

我使用此 pod 为我的应用程序添加深色模式。 https://github.com/draveness/NightNight

当我再次重新启动应用程序时它运行良好,但我想更改应用程序内部的主题。因此,我将 UISwitch 添加到我的侧面板,以便用户可以更改主题。

我为它添加了这段代码,有些颜色变化很好,但有些颜色不影响。例如 NavigationBar 背景颜色变化良好,但标题颜色没有变化。

UISwitch Action :

@IBAction func switchMode(_ sender: UISwitch) {

        if sender.isOn {
            switcher.isOn =  true

                NightNight.theme = NightNight.Theme.night
                UITabBar.appearance().barTintColor = UIColor(hexString: "#141d27")
                UITabBar.appearance().isTranslucent = true
                UITabBar.appearance().tintColor = UIColor(hexString: "#6e00ff")
                UINavigationBar.appearance().tintColor = UIColor(hexString: "#6e00ff")
                UINavigationBar.appearance().isTranslucent = true
                UINavigationBar.appearance().barTintColor = UIColor(hexString: "#141d27")


            for window in UIApplication.shared.windows {
                for view in window.subviews {
                    view.removeFromSuperview()
                    window.addSubview(view)
                }
            }

                UserDefaults.standard.set("night", forKey: "colormode")



        } else {
            switcher.isOn =  false

            NightNight.theme = NightNight.Theme.normal
            UITabBar.appearance().barTintColor = UIColor.white
            UITabBar.appearance().isTranslucent = true
            UITabBar.appearance().tintColor = UIColor(hexString: "#6e00ff")
            UINavigationBar.appearance().tintColor = UIColor(hexString: "#6e00ff")
            UINavigationBar.appearance().isTranslucent = true
            UINavigationBar.appearance().barTintColor = UIColor.white

            for window in UIApplication.shared.windows {
                for view in window.subviews {
                    view.removeFromSuperview()
                    window.addSubview(view)
                }
            }

            UserDefaults.standard.set("normal", forKey: "colormode")


        }

    }

enter image description here

enter image description here

enter image description here

enter image description here

通常灰色文本颜色(用户名和导航标题)在浅色模式下必须为黑色,但它们没有改变。

最佳答案

查看模式 更改时使用回调。 按预期更改颜色。

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    // do whatever you want to do 
}

关于ios - 选择深色模式后如何重新加载 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54989539/

相关文章:

ios - 使用台风注入(inject)单例提供多个实例

swift - 如何使用函数内部的 JSON 数据更新 Swift 3 中的标签?

ios - 方法 ':' 提供的 Objective-C 方法 ' ' 与协议(protocol)中可选要求方法 ' ' 冲突

swift - Swift 类函数和桥接 C 函数之间的名称冲突

ios - 消息 : "Fail! Play Again" does not display when score < 3

ios - 使用 AVSpeechSynthesizer 时如何在 iOS 7 应用程序中显示专辑封面?

objective-c - 隐藏动画模态 UIView?

ios - 持续时间不随 AVPlayer currentItem 一起提供

ios - 将文件从 bundle 复制到 Libraries 文件夹(IOS 10、Swift 3、xcode 8.2)

json - 在 Swift 4 中,如何使用 Codable 解码 JSON 并在解码对象之间创建引用?