如何在 SwiftUI 中使列表背景变黑?
How to make a List background black in SwiftUI?
我做的全屏黑屏就是用了这个构造在我看来:
ZStack {
Color.black.ignoresSafeArea() }
对于列表修饰符,我使用了以下两个:
.listRowBackground(Color.black)
.listStyle(.plain)
效果很好。
但是我想在我的列表顶部添加一个视图(如 header)并且它周围出现一些白色 space?那是从哪里来的?我怎样才能把它变成黑色?
这是我的完整代码:
struct WorkoutView: View {
var body: some View {
ZStack {
Color.black.ignoresSafeArea()
List {
TempView()
ForEach(1..<30) { index in
VStack(alignment: .leading, spacing: 4) {
Text("one line")
.foregroundColor(.white)
.font(.title)
.fontWeight(.bold)
}
.padding(.vertical)
}
.listRowBackground(Color.black)
}
.listStyle(.plain)
}
}
}
struct TempView: View {
var body: some View {
ZStack(alignment: .leading) {
Color.black.ignoresSafeArea()
Text("Hello World")
.foregroundColor(.red)
.font(.largeTitle)
.bold()
}
}
}
只需执行与 ForEach
相同的操作:添加 .listBackground()
修饰符。像这样:
TempView()
.listRowBackground(Color.black)
我做的全屏黑屏就是用了这个构造在我看来:
ZStack {
Color.black.ignoresSafeArea() }
对于列表修饰符,我使用了以下两个:
.listRowBackground(Color.black)
.listStyle(.plain)
效果很好。
但是我想在我的列表顶部添加一个视图(如 header)并且它周围出现一些白色 space?那是从哪里来的?我怎样才能把它变成黑色?
这是我的完整代码:
struct WorkoutView: View {
var body: some View {
ZStack {
Color.black.ignoresSafeArea()
List {
TempView()
ForEach(1..<30) { index in
VStack(alignment: .leading, spacing: 4) {
Text("one line")
.foregroundColor(.white)
.font(.title)
.fontWeight(.bold)
}
.padding(.vertical)
}
.listRowBackground(Color.black)
}
.listStyle(.plain)
}
}
}
struct TempView: View {
var body: some View {
ZStack(alignment: .leading) {
Color.black.ignoresSafeArea()
Text("Hello World")
.foregroundColor(.red)
.font(.largeTitle)
.bold()
}
}
}
只需执行与 ForEach
相同的操作:添加 .listBackground()
修饰符。像这样:
TempView()
.listRowBackground(Color.black)