A Working Solution:
All SwiftUI's List
s are backed by a UITableView
in iOS. so you need to change the background color of the tableView. But since Color
and UIColor
values are slightly different, you can get rid of the UIColor
.
struct ContentView: View {
init(){
UITableView.appearance().backgroundColor = .clear
}
@State var value = ""
var body: some View {
Form {
Section(header: Text("First Name")) {
TextField("First Name", text: $value)
}
Section(header: Text("Last Name")) {
TextField("Last Name", text: $value)
}
}
.foregroundColor(Color.blue)
.background(Color.yellow)
}
}
Now you can use Any background (including all Color
s) you want
Note that those top and bottom white areas are safe are and you can use .edgesIgnoringSafeArea()
modifier to get rid of them.
Restore
Since UITableView.appearance().backgroundColor
applies globally, you can use .onAppear
modifier to change it in different views (since it is a global change). So you can use another onAppear
or onDisappear
to reset it back to what you want.
And the default colors are:
UIColor.systemGroupedBackground
for the grouped style. And
UIColor.systemBackground
for the plain style.
And they both have automatic support for both dark mode and light mode.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…