gpt4 book ai didi

ios - App 开启深色模式

转载 作者:搜寻专家 更新时间:2023-11-01 06:31:45 26 4
gpt4 key购买 nike

我的项目 (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
}
}
}

最佳答案

使用用户默认值将开关状态保存在您的应用中:

@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
}
}

然后将所有 View 更改代码移动到每个 View Controller 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
}
}

关于ios - App 开启深色模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46369130/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com