使用列表视图设置视图背景颜色时遇到问题--SwiftUI
Having Problems Setting View Background Color with List View--SwiftUI
我想将视图背景颜色设置为绿色。我有一个列表视图,它很好,但底层视图需要是绿色的——见图。我知道我遗漏了一些东西,但使用 SwiftUI 时,您有时不知道它是错误还是它应该如何工作。任何对其他人有用的想法我都会很感激。我附上了我的代码,我使用的是 Xcode 版本 11.5 (11E608c)。
代码如下:
var body: some View {
ZStack() {
Color(red: 95.0/255.0, green: 122.0/255.0, blue: 128.0/255)
.edgesIgnoringSafeArea(.all)
Text("")
List {
Group {
Text("Price: ")
Text("Down: ")
Text("APR: ")
Text("Term: ")
Text("Tax: ")
Text("Rebate: ")
}.listRowBackground(Color(red: 95.0/255.0, green: 122.0/255.0, blue: 128.0/255))
Group {
Text("Add On: ")
Text("Fees: ")
Text("Trade Value: ")
Text("Total Interest: ")
Text("Payment: ")
}.listRowBackground(Color(red: 95.0/255.0, green: 122.0/255.0, blue: 128.0/255))
}
}
}
不幸的是 swiftui
还不支持这个,但是,List
只是一个 uitableview
的底层,所以你可以只更改 UITableView
属性。
struct Whosebug1: View {
// you need to modify the UItableview in the initializer
init() {
UITableView.appearance().backgroundColor = .yellow // Change background color
UITableViewCell.appearance().backgroundColor = .green // Change each cell's background color
}
var body: some View {
// your code goes here
}
}
重要的是要注意,这将更改此视图中的所有 List
个实例,因此如果您有多个列表,它们都将是相同的颜色。
要解决这个问题,您可以为每个列表制作不同的视图,然后将它们嵌入到您的主视图中。
代码如下:
var body: some View {
ZStack() {
Color(red: 95.0/255.0, green: 122.0/255.0, blue: 128.0/255)
.edgesIgnoringSafeArea(.all)
Text("")
List {
Group {
Text("Price: ")
Text("Down: ")
Text("APR: ")
Text("Term: ")
Text("Tax: ")
Text("Rebate: ")
}.listRowBackground(Color(red: 95.0/255.0, green: 122.0/255.0, blue: 128.0/255))
Group {
Text("Add On: ")
Text("Fees: ")
Text("Trade Value: ")
Text("Total Interest: ")
Text("Payment: ")
}.listRowBackground(Color(red: 95.0/255.0, green: 122.0/255.0, blue: 128.0/255))
}
}
}
不幸的是 swiftui
还不支持这个,但是,List
只是一个 uitableview
的底层,所以你可以只更改 UITableView
属性。
struct Whosebug1: View {
// you need to modify the UItableview in the initializer
init() {
UITableView.appearance().backgroundColor = .yellow // Change background color
UITableViewCell.appearance().backgroundColor = .green // Change each cell's background color
}
var body: some View {
// your code goes here
}
}
重要的是要注意,这将更改此视图中的所有 List
个实例,因此如果您有多个列表,它们都将是相同的颜色。
要解决这个问题,您可以为每个列表制作不同的视图,然后将它们嵌入到您的主视图中。