首页 > 解决方案 > 有没有办法通过使用用户默认值来更改我所有的 IOS 应用程序页面的背景颜色?

问题描述

我正在关注 Youtube 上提供的本教程:如何使用 UserDefaults 保存数据 - Swift

https://www.youtube.com/watch?v=sUhq1vIrRbo

而且我有这个代码只适用于一个页面,并且想知道如何做同样的事情(改变背景颜色),但我的整个应用程序页面都基于用户的选择。

我尝试将 checkForStylePreference() 保留在另一个页面的 viewDidLoad() 中,但它无法识别它。我复制粘贴了整个 checkForStylePreference() 但仍然缺少其他代码。唯一的方法是在所有 App 页面中复制粘贴 viewController 的所有方法吗?还是有一种更简单的方法可以减少代码量?目前我可以将 BgColor 从白色更改为灰色完美地在此处输入图像描述,但我不知道如何将其应用于所有人。

这是我的 NameViewController.swift 的代码(我为屏幕截图中的页面创建的代码)。请注意,我还有 2 个 swift 文件,它们是 SAButton.swift 和 ConstantStyles.swift(用于颜色)

class NameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        nameLbl.text = myString
        checkForStylePreference()

    }

    @IBAction func didChangeStyleSeg(_ sender: UISegmentedControl) {
        isDarkMode = sender.selectedSegmentIndex == 1
        saveStylePreference()
        updateStyle()
    }

    var myString = String()
    @IBOutlet weak var styleSegment: UISegmentedControl!
    @IBOutlet weak var nameLbl: UILabel!


        var isDarkMode = false
        let defaults = UserDefaults.standard


        struct Keys {
            static let preferDarkMode = "preferDarkMode"

        }


        func updateStyle(){

            UIView.animate(withDuration: 0.4){
               // self.view.backgroundColor = self.isDarkMode ? Colors.darkGrey : .white
               // UIColor(hue: 287/360, saturation: 15/100, brightness: 85/100, alpha: 1.0) 
        self.view.backgroundColor = self.isDarkMode ? Colors.lightGrey : .white

                //recent correct one
                // self.view.backgroundColor = self.isDarkMode ? Colors.darkGrey : .white

                //self.view.UIBackgroundFetchResult = self.isDarkMode? UIColor.grey : .white

            }
        }


        func saveStylePreference(){
            defaults.set(isDarkMode, forKey: Keys.preferDarkMode)


        }


        func checkForStylePreference(){
            let preferDarkMode = defaults.bool(forKey: Keys.preferDarkMode)
            if preferDarkMode{
                isDarkMode = true
                updateStyle()
                styleSegment.selectedSegmentIndex = 1


            }

        }


        // Do any additional setup after loading the view.
    }

SAButton.swift 的代码

class SAButton: UIButton {


    override init(frame: CGRect) {
        super.init(frame: frame)
        setupButton()
    }


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupButton()
    }


    private func setupButton() {
        setTitleColor(.white, for: .normal)
        backgroundColor     = Colors.lightBlue
        titleLabel?.font    = .boldSystemFont(ofSize: 20)
        layer.cornerRadius  = frame.size.height / 2
    }


}

ConstantStyles.swift 的代码

import UIKit

struct Colors {
    static let darkGrey  = UIColor(red: 40/255, green: 40/255, blue: 40/255, alpha: 1)
   // static let purple = UIColor(red: 212/255, green: 186/255, blue: 86/255, alpha: 1)
    static let lightBlue = UIColor(red: 89/255, green: 205/255, blue: 242/255, alpha: 1)
    static let darkPurple = UIColor(red: 242/255, green: 232/255, blue: 255/255, alpha: 1.0)
         // UIColor(hue: 287/360, saturation: 15/100, brightness: 85/100, alpha: 1.0)
    static let lightPurple = UIColor(red: 240/255, green: 229/255, blue: 255/255, alpha: 1.0)
    static let lightGrey = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1.0)
        //UIColor(red: 249/255, green: 244/255, blue: 255/255, alpha: 1.0)

}

我相信这可能很简单,但我是 Swift 新手,我想知道代码的哪一部分要准确保留以及保留在哪里。非常感激。

Ps:原项目源码在Youtube视频下方提供。

标签: iosswift

解决方案


您可以创建一个主类并从中继承

class GeneralVC : UIViewController {
    override func viewDidLoad() {
        self.view.backgroundColor = .red // read color from userdefaults and set it here
    }
}
class ViewController: GeneralVC {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
}

这同样适用于您需要全局影响的任何UIKit组件


推荐阅读