将黑暗模式添加到iOS应用程序 [英] Adding dark mode to iOS app

查看:167
本文介绍了将黑暗模式添加到iOS应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用添加一个主题(一个黑暗的主题)。因此,当用户单击活动开关时,它将使整个应用程序进入黑暗模式。我已经对黑暗模式进行了硬编码,只是为了看看它的样子;但是现在我希望能够通过和UISwitch启用和禁用它,但我不知道该怎么做?

I am trying to add a theme to my app (a dark theme). So when the user clicks an activity switch it would then make the whole app go into the dark mode. I have hard coded the dark mode just to see what it would look like; however now I would like to be able to enable and disable it through and UISwitch, but I am not sure how to do this?

class DarkModeTableViewCell: UITableViewCell {

var DarkisOn = Bool()
let userDefaults = UserDefaults.standard


@IBOutlet var darkModeSwitchOutlet: UISwitch!

override func awakeFromNib() {
    super.awakeFromNib()


}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}


@IBAction func darkModeSwitched(_ sender: Any) {

    if darkModeSwitchOutlet.isOn == true {

        //enable dark mode

        DarkisOn = true

        userDefaults.set(true, forKey: "DarkDefault")
        userDefaults.set(false, forKey: "LightDefault")



    } else {

        //enable light mode
        DarkisOn = false

        userDefaults.set(false, forKey: "DarkDefault")
        userDefaults.set(true, forKey: "LightDefault")
    }

}



}



class DarkModeViewController: UIViewController {



func set(for viewController: UIViewController) {



    viewController.view.backgroundColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 1.0)
        viewController.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    viewController.navigationController?.navigationBar.tintColor =     UIColor.white
    viewController.navigationController?.navigationBar.barStyle =     UIBarStyle.black
    viewController.tabBarController?.tabBar.barStyle = UIBarStyle.black






}
static let instance = DarkModeViewController()
}

然后我所做的就是调用每个函数其中一个视图控制器可以看到它的外观,但是我需要能够在开关打开或关闭的情况下访问bool值,如果是,那么它可以执行该功能,否则只需保持相同。如果您有任何其他问题,请告诉我,我知道其中一些可能没有多大意义。

and then what I do is call the function in each one of the view controllers to see what it looks like, but I need to be able to access the bool value on if the switch is on or off and if it is then have it do that function, otherwise to just keep things the same. If you have any further questions, please let me know, I know some of this might not make to much sense.

推荐答案

我使用通知( NSNotificationCenter API)来解决这个问题。

I'd solve this using Notifications (NSNotificationCenter APIs).

我们的想法是通过实时通知您的视图控制器启用暗模式和禁用暗模式时,它们也可以实时适应变化。您无需检查交换机的状态或类似的内容。

The idea is to notify your view controllers in real-time when the dark mode is enabled and when it is disabled, so they can also adapt to the change in real time. You don't need to check the status of the switch or anything like that.

首先创建两个通知(您也可以只使用一个通知并传入 userInfo 字典中的所需主题,但在这种情况下,创建两个通知会更容易,因为你需要使用Swift进行强制转换和使用。

Start by creating two notifications (you can also do it with one only and pass in the desired theme in the userInfo dictionary, but in this case it's easier to create two notifications, since you need to cast and what-not with Swift).

NotificationsName + Extensions.swift

import Foundation

extension Notification.Name {
    static let darkModeEnabled = Notification.Name("com.yourApp.notifications.darkModeEnabled")
    static let darkModeDisabled = Notification.Name("com.yourApp.notifications.darkModeDisabled")
}

关于你所有的问题 查看控制器,收听这些通知:

On all your "themable" view controllers, listen to these notifications:

    override func viewDidLoad() {
        super.viewDidLoad()

        // Add Observers
        NotificationCenter.default.addObserver(self, selector: #selector(darkModeEnabled(_:)), name: .darkModeEnabled, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(darkModeDisabled(_:)), name: .darkModeDisabled, object: nil)
    }

不要忘记在<$ c $中删除它们c> deinit ,因为向无效对象发送通知会引发异常:

Don't forget to remove them in deinit, since sending notifications to invalid objects raises an exception:

deinit {
    NotificationCenter.default.removeObserver(self, name: darkModeEnabled, object: nil)
    NotificationCenter.default.removeObserver(self, name: darkModeDisabled, object: nil)
}

在themable视图控制器中,实现 darkModeEnabled(_:) darkModeDisabled(_:)

In your "themable" view controllers, implement darkModeEnabled(_:) and darkModeDisabled(_:):

@objc private func darkModeEnabled(_ notification: Notification) {
    // Write your dark mode code here
}

@objc private func darkModeDisabled(_ notification: Notification) {
    // Write your non-dark mode code here
}

最后,切换你的开关将触发通知:

Finally, toggling your switch will trigger either notification:

@IBAction func darkModeSwitched(_ sender: Any) {

    if darkModeSwitchOutlet.isOn == true {

        //enable dark mode

        DarkisOn = true

        userDefaults.set(true, forKey: "DarkDefault")
        userDefaults.set(false, forKey: "LightDefault")

        // Post the notification to let all current view controllers that the app has changed to dark mode, and they should theme themselves to reflect this change.
        NotificationCenter.default.post(name: .darkModeEnabled, object: nil)

    } else {

        //enable light mode
        DarkisOn = false

        userDefaults.set(false, forKey: "DarkDefault")
        userDefaults.set(true, forKey: "LightDefault")

        // Post the notification to let all current view controllers that the app has changed to non-dark mode, and they should theme themselves to reflect this change.
        NotificationCenter.default.post(name: .darkModeDisabled, object: nil)
    }

}

这样,当主题发生变化时,所有视图控制器都会实时通知,他们会做出相应的反应。请注意,您需要采取措施在应用程序启动时显示正确的模式,但我确信您正在这样做,因为您正在使用UserDefaults并且可能会检查它们。另外值得一提的是NSNotificationCenter不是线程安全的,尽管它应该无关紧要,因为所有的UI代码都应该放在主线程中。

With this, all your view controllers will be notified in real time when the "theme" changes and they will react accordingly. Do note that you need to take measures to show the right mode when the app launches, but I'm sure you are doing that since you are using UserDefaults and presumably checking them. Also worth mentioning NSNotificationCenter is not thread-safe, although it shouldn't matter since this all UI code that should go in the main thread anyway.

有关更多信息,请参阅可以查看 NSNotificationCenter文档

For more information, you can check the NSNotificationCenter documentation.

这篇关于将黑暗模式添加到iOS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆