我创建了一个开关,让用户可以更改背景颜色(暗模式)。这仅适用于与代码链接的视图控制器。当我将开关激活到暗或亮模式时,我将如何设置它,我的应用程序中的每个视图控制器都会改变,而不仅仅是一个。继承我的代码:
import UIKit
类DarkMode:UIViewController {
@IBOutlet var DarkSwitch: UISwitch!
@IBOutlet var LightSwitch: UISwitch!
var DarkisOn = Bool()
var LightisOn = Bool()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let DarkDefault = UserDefaults.standard
DarkisOn = DarkDefault.bool(forKey: "DarkDefault")
let LightDefault = UserDefaults.standard
LightisOn = LightDefault.bool(forKey: "LightDefault")
if (DarkisOn == true) {
DarkSwitch.isOn = true
LightSwitch.isOn = false
//run dark theme
DarkTheme()
}
if (LightisOn == true) {
DarkSwitch.isOn = false
LightSwitch.isOn = true
//run light theme
LightTheme()
}
}
func DarkTheme() //dark colour
{
self.view.backgroundColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 1.0)
}
func LightTheme() //light colour
{
self.view.backgroundColor = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
@IBAction func DarkAction(_ sender: Any)
{
DarkSwitch.isOn = true
LightSwitch.isOn = false
//run dark theme func
DarkTheme()
let DarkDefault = UserDefaults.standard
DarkDefault.set(true, forKey: "DarkDefault")
let LightDefault = UserDefaults.standard
LightDefault.set(false, forKey: "LightDefault")
}
@IBAction func LightAction(_ sender: Any)
{
DarkSwitch.isOn = false
LightSwitch.isOn = true
//run light theme func
LightTheme()
let DarkDefault = UserDefaults.standard
DarkDefault.set(false, forKey: "DarkDefault")
let LightDefault = UserDefaults.standard
LightDefault.set(true, forKey: "LightDefault")
}
}
答案 0 :(得分:1)
对于我的一个项目,我创建了一个控制所有ViewControllers的UI颜色方案的类。
class UIColourScheme {
func set(for viewController: UIViewController) {
viewController.view.backgroundColor = bgColour
...
}
var bgColour = UIColor.black
static let instance = UIColourScheme()
}
然后我会在viewDidLoad()中为每个ViewController调用此函数
class MyViewController : UIViewController {
func viewDidLoad() {
...
UIColourScheme.instance.set(for:self)
}
}
我的配色方案为所有设置了颜色,但它可以简化为上面的背景颜色。
答案 1 :(得分:0)
你可以像这样创建一个基类(这是我用过的东西。)
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.barTintColor = MainColor
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]//user global variable
self.navigationController?.navigationBar.barStyle = UIBarStyle.black //user global variable
self.navigationController?.navigationBar.tintColor = UIColor.white //user global variable
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
在此处设置您的所有主题。 所有颜色值都应该是全局的,您可以从另一个ViewController更改。
现在宣布你的所有ViewControllers都是这样的
class HomeViewController: BaseViewController {
}
这样HomeViewController将具有您在BaseViewController中设置的所有外观。
现在你所要做的就是改变颜色值的全局变量。
答案 2 :(得分:0)
您可以使用
protocol colorable {
func setcolor(color: UIColor)
}
class HomeVC: colorable {
}