使用开关在整个应用程序中更改文本,单元格和视图控制器的颜色

问题描述 投票:1回答:1

我正在尝试将夜间模式集成到我的应用程序中并部分使其正常工作。这是我的代码以及使用夜间模式开关时应用程序如何更改的屏幕截图。设置屏幕是完美的,但前两个视图控制器没有按照预期的全黑背景改变颜色。

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

        }

    }

}

App before and after night mode switch is toggled

ios swift view uicolor uiswitch
1个回答
0
投票

如果您希望更改在全球范围内发生,我建议采用以下方法:

// 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
    ...

}

在实际开关所在的视图控制器中,您可以为通知观察器的操作方法中的更改设置动画,我认为这是一个很好的触摸(即Twitter)。

© www.soinside.com 2019 - 2024. All rights reserved.