Colors SwiftUI:VStack don';不适用于白色背景色,但每隔一种颜色
我有以下代码。如果我将VStack的Colors SwiftUI:VStack don';不适用于白色背景色,但每隔一种颜色,colors,swiftui,vstack,Colors,Swiftui,Vstack,我有以下代码。如果我将VStack的.background(Color.green)更改为.background(Color.white),则背景将是我用于Listbackground的系统灰色 它与.colorMultiply(Color(UIColor.systemGray4))属性有关吗 var body: some View { NavigationView { List { Text("Bla bla bla")
.background(Color.green)
更改为.background(Color.white)
,则背景将是我用于Listbackground的系统灰色
它与.colorMultiply(Color(UIColor.systemGray4))
属性有关吗
var body: some View {
NavigationView {
List {
Text("Bla bla bla")
Group {
VStack {
TextField("Server address", text: $serverAddress)
.keyboardType(.default)
TextField("Server port", text: $serverPortString)
.keyboardType(.numberPad)
}
}
.padding()
.background(Color.green)
.cornerRadius(8)
// Some more elements
}
.navigationBarHidden(false)
.navigationBarTitle("Connect your Server", displayMode: .large)
}
.colorMultiply(Color(UIColor.systemGray4))
.onTapGesture {
self.hideKeyboard()
}
被忽略的白色背景:
工作绿色背景:
.colorMultiply()
将颜色倍增效果添加到应用它的视图中,这意味着它会将其着色为您添加的颜色。由于您的NavigationView
是包含视图,因此其中的所有视图都将着色为systemGray4
当您将其设置为白色时,systemGray4
会着色,使其与背景融为一体,因为背景也是白色的
你可以这样做:
struct ContentView: View {
@State var serverAddress: String = ""
@State var serverPortString: String = ""
let backgroundColor = Color.init(UIColor.systemGray4)
init() {
UITableView.appearance().backgroundColor = .clear // or you could set this to systemGray4 and ignore the ZStack
UITableView.appearance().separatorColor = .clear
UITableView.appearance().tableFooterView = UIView()
UITableView.appearance().separatorStyle = .none
}
var body: some View {
NavigationView {
ZStack {
backgroundColor.frame(maxWidth: .infinity, maxHeight: .infinity).edgesIgnoringSafeArea(.all)
List {
Text("Bla bla bla")
.listRowBackground(backgroundColor)
Group {
VStack {
TextField("Server address", text: $serverAddress)
.keyboardType(.default)
TextField("Server port", text: $serverPortString)
.keyboardType(.numberPad)
}
}
.listRowBackground(backgroundColor)
.padding()
.background(Color.white)
.cornerRadius(8)
// Some more elements
}
}
.navigationBarHidden(false)
.navigationBarTitle("Connect your Server", displayMode: .large)
}
.onTapGesture {
print("tapped")
}
}
}
这将为您提供以下结果:
谢谢,但是如何更改背景色呢?因为just.background(Color.white)不会修改任何内容。还有UITableView.appearance().backgroundColor我觉得不太好。我在答案中添加了一些代码。我认为要获得所需的结果,您需要操作
UITableView.appearance()
谢谢:)但是没有全局更改UITableView外观的其他方法吗?我认为用这种方式操纵它并不是一种“干净”的方式。不幸的是,现在没有其他方式可以做到这一点。您可以在onAppear
中更改UITableView.appearance()
,然后在onDisappear
中重置它,而不是在init
中更改它。