【问题标题】:Dark Mode Switch on App暗模式开启应用
【发布时间】:2017-09-22 16:18:35
【问题描述】:

我的项目 (Swift) 中有一个 TableViewController 和一个 ViewController。我有一个开关可以改变我的应用程序的颜色(变暗)。问题是它只在我所在的场景中改变它。如果我去另一个场景,它是白色的。

我的代码:

import UIKit

class BaseTableViewController: UITableViewController {
    @IBOutlet var InicioTable: UITableView!
    @IBOutlet weak var cell2: UITableViewCell!
    @IBOutlet var viewTable: UITableView!
    @IBOutlet weak var celldarkmode: UITableViewCell!
    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var switchController: UISwitch!

    @IBAction func changeSwitch(_ sender: UISwitch) {
        if switchController.isOn == true
        {
            self.navigationController?.navigationBar.isTranslucent = false
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]//user global variable
            self.navigationController?.navigationBar.barStyle = UIBarStyle.black //user global variable
            self.navigationController?.navigationBar.tintColor = UIColor.black //user global variable
            UIApplication.shared.statusBarStyle = .lightContent
            label.textColor = UIColor.white
            self.cell2.backgroundColor = UIColor.black
            self.tabBarController?.tabBar.barTintColor = UIColor.black
            view.backgroundColor = UIColor.init(red: 0.1, green: 0.1, blue: 0.1, alpha: 1.0)
        }
        else
        {
            self.navigationController?.navigationBar.isTranslucent = false
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]//user global variable
            self.navigationController?.navigationBar.barStyle = UIBarStyle.default //user global variable
            self.navigationController?.navigationBar.tintColor = UIColor.white //user global variable
            UIApplication.shared.statusBarStyle = .default
            label.textColor = UIColor.black
            self.cell2.backgroundColor = UIColor.white
            self.tabBarController?.tabBar.barTintColor = UIColor.white
            view.backgroundColor = UIColor.groupTableViewBackground
        }
    }
}

【问题讨论】:

  • 包含一些代码来显示你在做什么。通常,您需要在视图之间复制切换状态,方法是使用可以捕获目标视图控制器并在显示之前设置属性的segue,或者将切换状态保存到UserDefaults,或者通过实现单例状态类。
  • 我在这里粘贴了我的代码。因为我是新手,所以带有屏幕的说明对我来说更好,但我会记下这一点。

标签: ios swift


【解决方案1】:

使用用户默认设置在您的应用中保存该开关状态:

@IBAction func changeSwitch(_ sender: UISwitch) {
    let isDarkMode = userDefaults.bool(forKey: "isDarkMode")
    if isDarkMode == true {
        UserDefaults.standard.set(false, forKey: "isDarkMode")  // Set the state
    }
    else {
        UserDefaults.standard.set(true, forKey: "isDarkMode")  // Set the state
    }
}

然后将所有视图更改代码移动到您希望更改颜色的每个视图控制器viewDidLoad()

override func viewDidLoad() {

    super.viewDidLoad()

    let isDarkMode = UserDefaults.standard.bool(forKey: "isDarkMode")  // Retrieve the state

    if isDarkMode == true {
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]//user global variable
        self.navigationController?.navigationBar.barStyle = UIBarStyle.black //user global variable
        self.navigationController?.navigationBar.tintColor = UIColor.black //user global variable
        UIApplication.shared.statusBarStyle = .lightContent
        label.textColor = UIColor.white
        self.cell2.backgroundColor = UIColor.black
        self.tabBarController?.tabBar.barTintColor = UIColor.black
        view.backgroundColor = UIColor.init(red: 0.1, green: 0.1, blue: 0.1, alpha: 1.0)
    }
    else {
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black]//user global variable
        self.navigationController?.navigationBar.barStyle = UIBarStyle.default //user global variable
        self.navigationController?.navigationBar.tintColor = UIColor.white //user global variable
        UIApplication.shared.statusBarStyle = .default
        label.textColor = UIColor.black
        self.cell2.backgroundColor = UIColor.white
        self.tabBarController?.tabBar.barTintColor = UIColor.white
        view.backgroundColor = UIColor.groupTableViewBackground
    }
}

【讨论】:

  • 嘿Shades,谢谢你的帮助,但我其实没听懂你,你能不能一步一步给我解释一下?或发送屏幕截图,比如我不知道如何在 ViewController 上检索它之类的东西,谢谢。
  • @A.Samuel 没有可截图的内容。您正在保存用户选择的首选项(深色或浅色),然后在每个视图控制器中检索已保存的设置。阅读用户默认值:stackoverflow.com/questions/31203241/…
  • 哦等等发生了什么事,我关闭了应用程序,现在黑暗模式是永久的,我的意思是,它现在是黑色的,即使我点击开关,它也不会变成白色。跨度>
  • @A.Samuel 如果您希望立即更改颜色,请将代码复制回 IBAction(或者最好将其全部放在函数中以供重用),但也要确保保留它在 viewdidLoad 中。
  • 好的,现在当我点击开关时,它只会在那个场景中将颜色变回白色,但它会让另一个场景变暗。
猜你喜欢
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
  • 2020-01-02
  • 1970-01-01
  • 2022-08-10
  • 2020-01-31
  • 2022-01-26
  • 2019-04-04
相关资源
最近更新 更多