SwiftUI 列表背景的默认颜色是什么?
What's the default color for the background of a SwiftUI List?
我知道如何更改 SwiftUI 视图列表的背景颜色,但我找不到默认颜色。我试过使用 MacOS 'Digital Color Meter',但它就是不正确。
正如您在这张图片中看到的,我尝试设置列表行的背景颜色(使用 .listRowBackground
与基于数字色度计的值的周围列表完全相同.
真的有人知道默认背景颜色是什么吗?
每个 ListStyle 和平台的默认颜色都不同,我认为 SwiftUI 不会公开这些颜色。从技术上讲,您可以 Introspect 列表视图并通过 UIKit 获取它的背景颜色。
简答:好像是UIColor.secondarySystemBackground
长答案:
我已经试过 Digital Color Meter app 和你一样。它显示此 RGB 值:242、242、247。
我创造了这样的颜色:
这是我的代码:
import SwiftUI
extension Color {
public static let ListBGColor = Color("ListBGColor")
}
struct ContentView: View {
var body: some View {
List {
ForEach(0..<5) {_ in
Text("Hello, world!")
.padding()
}
.listRowBackground(Color.ListBGColor)
}
.listStyle(InsetGroupedListStyle())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
结果:与背景颜色相同(模拟器和 iPhone 上的睾丸):
我知道如何更改 SwiftUI 视图列表的背景颜色,但我找不到默认颜色。我试过使用 MacOS 'Digital Color Meter',但它就是不正确。
正如您在这张图片中看到的,我尝试设置列表行的背景颜色(使用 .listRowBackground
与基于数字色度计的值的周围列表完全相同.
真的有人知道默认背景颜色是什么吗?
每个 ListStyle 和平台的默认颜色都不同,我认为 SwiftUI 不会公开这些颜色。从技术上讲,您可以 Introspect 列表视图并通过 UIKit 获取它的背景颜色。
简答:好像是UIColor.secondarySystemBackground
长答案:
我已经试过 Digital Color Meter app 和你一样。它显示此 RGB 值:242、242、247。
我创造了这样的颜色:
这是我的代码:
import SwiftUI
extension Color {
public static let ListBGColor = Color("ListBGColor")
}
struct ContentView: View {
var body: some View {
List {
ForEach(0..<5) {_ in
Text("Hello, world!")
.padding()
}
.listRowBackground(Color.ListBGColor)
}
.listStyle(InsetGroupedListStyle())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
结果:与背景颜色相同(模拟器和 iPhone 上的睾丸):