SwiftUI的暗黑模式适配方案
import SwiftUI
struct DarkModelBootCamp: View {
/// 获取环境变量的颜色值
@Environment(\.colorScheme) var colorScheme
var body: some View {
NavigationView {
ScrollView{
Text("this is primary")
.foregroundColor(.primary)
Text("this is secondary")
.foregroundColor(.secondary)
Text("this is black")
.foregroundColor(.black)
Text("this is white")
.foregroundColor(.white)
Text("this is red")
.foregroundColor(.red)
/// 使用自定义颜色的两种方式,首先要去。xcassets文件管理自己的自定义颜色
Text("this is customer")
.foregroundColor(Color.custom)
Text("this is customer")
.foregroundColor(Color("customColor"))
/// 手动通过读取环境的模式来切换色值
Text("this is adaptive")
.foregroundColor(colorScheme == .light ? .yellow : .orange)
}
.navigationTitle("DarkModelBootCamp")
}
}
}
#Preview {
DarkModelBootCamp()
.preferredColorScheme(.dark)
}
第一种 通过在xcassets文件管理自己的颜色方案
第二种 在代码中,获取当前环境的模式枚举,去管理的自己的一套配色方案
第三种 就是选择不适配只显示光模式