Agregar modo oscuro a la aplicación iOS
Estoy tratando de agregar un tema a mi aplicación (un tema oscuro). Entonces, cuando el usuario hace clic en un interruptor de actividad, la aplicación completa pasará al modo oscuro. He codificado el modo oscuro solo para ver cómo se vería; sin embargo, ahora me gustaría poder habilitarlo y deshabilitarlo a través de UISwitch, pero no estoy seguro de cómo hacerlo.
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()
}
y luego lo que hago es llamar a la función en cada uno de los controladores de vista para ver cómo se ve, pero necesito poder acceder al valor de bool si el interruptor está activado o desactivado y si lo está, haga que lo haga esa función, de lo contrario, simplemente mantener las cosas igual. Si tiene más preguntas, hágamelo saber, sé que algo de esto podría no tener mucho sentido.