简体   繁体   English

选择暗模式后如何重新加载视图?

[英]How can I reload views after choose dark mode?

Im using this pod for add dark mode to my app.我正在使用这个 pod 为我的应用程序添加黑暗模式。 https://github.com/draveness/NightNight https://github.com/draveness/NightNight

Its working well when I restart app again but I want to change theme inside of app.当我再次重新启动应用程序时它运行良好,但我想更改应用程序内部的主题。 So, I added UISwitch to my sidepanel for user can change theme.因此,我将 UISwitch 添加到我的侧面板中,以便用户可以更改主题。

I added this codes for it and some colors changing well but some colors does not affect.我为它添加了这个代码,有些颜色变化很好,但有些颜色不影响。 For example NavigationBar background color is changing well but title color is not changing.例如 NavigationBar 背景颜色变化良好,但标题颜色没有变化。

UISwitch Action: UISwitch 动作:

@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")


        }

    }

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Normally gray text colors(User`s names and navigation title) must to be black in light mode, but they does not changed.通常灰色文本颜色(用户名和导航标题)在浅色模式下必须为黑色,但它们不会改变。

Use callback when view mode changes.view mode改变时使用回调。 Change your colors as expected.按预期更改颜色。

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

Check out RxSwift and RxCocoa.查看 RxSwift 和 RxCocoa。 They can both be found here .他们都可以在这里找到。 They are Reactive Frameworks for Reactive Programming in Swift.它们是 Swift 中反应式编程的反应式框架。 You can create observables to change the color of views reactively, as needed.您可以根据需要创建 observable 以被动地更改视图的颜色。

暂无
暂无

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

相关问题 如何检查 iOS/iPadOS 中是否启用了暗模式? - How can I check whether dark mode is enabled in iOS/iPadOS? SwiftUI 如何实现暗模式切换和刷新所有视图 - SwiftUI How to implement dark mode toggle and refresh all views 如果设备处于深色模式,如何将 iOS 中的文本指定为白色,如果处于浅色模式,如何将文本指定为黑色? - How can I specify text in iOS to be white if the device is in dark mode and black if in light mode? 如何在 iOS 设置包中为 slider 指定暗/亮模式图像? - How can I specify dark/light mode images for a slider in an iOS settings bundle? 如何使用 SwiftUI 在 iOS 13 暗模式下采用应用程序? - How can I adopt apps to iOS 13 Dark mode using SwiftUI? 下载json后如何重新加载表格 - How can i reload the table after download json 暗/亮模式更改时可以重新加载应用程序吗? - Possible to reload app when dark/light mode changes? 当 UITableViewCell 处于编辑模式并且我们重新加载 UITableView 时,即使在重新加载后如何保持该特定单元格处于编辑模式 - When a UITableViewCell is in editing mode and we reload the UITableView, how to keep that particular cell in editing mode even after reload 视图背后的魔法自动适应 ios13 暗模式? - Magic behind views automatically adapting to ios13 dark mode? 如何在 iOS 的原生反应中动态地将亮模式更改为暗模式? - how do I dynamically change light mode to dark mode in react native in iOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM