我正在尝试将夜间模式集成到我的应用程序中并已部分运行,这是我的代码和使用夜间模式开关时应用程序如何变化的屏幕截图。设置屏幕是完美的,但前两个 View Controller 并没有像预期的那样改变全黑背景的颜色。
import UIKit
var selectedRunesArray = runesIncReversedArray
class SettingsViewController: UIViewController {
@IBOutlet weak var allowReversedRunesLabel: UILabel!
@IBOutlet weak var allowDuplicateRunesLabel: UILabel!
@IBOutlet weak var nightModeLabel: UILabel!
@IBOutlet weak var allowReversedRunesSwitch: UISwitch!
@IBOutlet weak var allowDuplicateRunesSwitch: UISwitch!
@IBOutlet weak var nightModeSwitch: UISwitch!
let defaults = UserDefaults.standard
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if let revRunes = defaults.value(forKey: "reversedRunes") {
allowReversedRunesSwitch.isOn = revRunes as! Bool
}
if let dupRunes = defaults.value(forKey: "duplicateRunes") {
allowDuplicateRunesSwitch.isOn = dupRunes as! Bool
}
if let nMode = defaults.value(forKey: "nightMode") {
nightModeSwitch.isOn = nMode as! Bool
}
}
@IBAction func reversedRunesChanged(_ sender: UISwitch) {
defaults.set(sender.isOn, forKey: "reversedRunes")
if allowReversedRunesSwitch.isOn == true {
selectedRunesArray = runesIncReversedArray
} else {
selectedRunesArray = runesArray
}
}
@IBAction func duplicateRunesChanged(_ sender: UISwitch) {
defaults.set(sender.isOn, forKey: "duplicateRunes")
if allowDuplicateRunesSwitch.isOn == true {
} else {
}
}
@IBAction func nightModeChanged(_ sender: UISwitch) {
defaults.set(sender.isOn, forKey: "nightMode")
if nightModeSwitch.isOn == true {
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
self.navigationController?.navigationBar.barStyle = UIBarStyle.black
self.navigationController?.navigationBar.tintColor = UIColor.black
UIApplication.shared.statusBarStyle = .lightContent
allowReversedRunesLabel.textColor = UIColor.white
allowDuplicateRunesLabel.textColor = UIColor.white
nightModeLabel.textColor = UIColor.white
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]
self.navigationController?.navigationBar.barStyle = UIBarStyle.default
self.navigationController?.navigationBar.tintColor = UIColor.white
UIApplication.shared.statusBarStyle = .default
allowReversedRunesLabel.textColor = UIColor.black
allowDuplicateRunesLabel.textColor = UIColor.black
nightModeLabel.textColor = UIColor.black
self.tabBarController?.tabBar.barTintColor = UIColor.white
view.backgroundColor = UIColor.groupTableViewBackground
}
}
}
最佳答案
如果您希望在全局范围内进行更改,我建议采用以下方法:
// an extension here is helpful
extension Notification.Name {
static let dayOrNight = Notification.Name(rawValue: "dayOrNight")
}
// add the switch and set its state from the current user default
func addNightModeSwitch() {
toggle.addTarget(self, action: #selector(nightModeSwitchAction(_:)), for: .valueChanged)
if UserDefaults.standard.bool(forKey: "nightMode") {
toggle.isOn = true
} else {
toggle.isOn = false
}
...
}
// post a notification when the switch is switched
@objc func nightModeSwitchAction(_ toggle: UISwitch) {
if toggle.isOn {
// update user default
UserDefaults.standard.set(true, forKey: "nightMode")
// post notification
NotificationCenter.default.post(name: .dayOrNight, object: nil)
} else {
// update user default
UserDefaults.standard.set(false, forKey: "nightMode")
// post notification
NotificationCenter.default.post(name: .dayOrNight, object: nil)
}
}
// add a notification observer to any view controller where you want UI changes
func addNotificationObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(dayOrNightHandler), name: .dayOrNight, object: nil)
}
// make sure all of the objects that need changes are instance properties of the view controller and change them through the notification observer's action
@objc func dayOrNightHandler() {
view.backgroundColor = UIColor.black
someButton.backgroundColor = UIColor.white
...
}
并且在实际开关所在的 View Controller 中,您可以为通知观察者的操作方法中的变化设置动画,我认为这是一个很好的接触(即 Twitter)。
关于ios - 使用开关更改整个应用程序中文本、单元格和 View Controller 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47876261/