目前,我正在使用SwiftUI,因为我支持深色模式,所以我声明了一种颜色集,如果它处于亮/暗模式,则视图必须符合该颜色集。
这当然需要在每个组件中进行很多更改,现在我有很多包含此代码的视图
init() {
// Make Dividers the same color as the background to make them disappear
UITableView.appearance().separatorColor = UIColor(named: "background")
// Set List background color
UITableView.appearance().backgroundColor = UIColor(named: "background")
UIPickerView.appearance().backgroundColor = UIColor(named: "background")
}
我曾尝试扩展单个组件,但我一定做错了什么,我也曾尝试扩展View,但是每次构建都失败,有没有一种方法可以在一个文件中声明它并对其进行全局更改,因此我可以清除所有这些冗余代码?
从我所见,这实际上是一个非常简单的问题,但我在iOS世界中并不怎么了解,所以...
解决方案是将所有代码放入AppDelegate文件中,特别是在其中
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool