SwiftUi - 无法更改/无法访问背景
SwiftUi - Background can't be changed / can't be reached
我的背景有问题。
自从我玩了几天 SwiftUi,一切都很好。
昨天我用背景填满了我的屏幕,但只有顶部配件不起作用,所以我撤消了我的更改。现在的问题是:我无法再访问我的背景...
其实我的后台是达不到的,所以没法改
这是我的代码:
struct ContentView : View {
@State private var name = String()
@State private var pw = String()
var body: some View {
VStack{
NavigationView {
Spacer()
//Einleitung
VStack{
Image("user")
.resizable()
.frame(width: 100 , height: 100)
Spacer()
Text("Hello User.")
Text("Please insert your Nickname and your Password")
.lineLimit(nil)
}.background(Color.blue)
.padding()
//TextField eingabe für Name + Passwort
HStack{
Spacer()
Text("Username")
.font(.headline)
Spacer()
Text("Password")
.font(.headline)
Spacer()
}.background(Color.red)
HStack{
Spacer()
TextField($name,placeholder: Text("Fill your name"))
.textFieldStyle(.roundedBorder)
.scaledToFit()
Spacer()
SecureField($pw, placeholder: Text("123456789"))
.scaledToFit()
.textFieldStyle(.roundedBorder)
Spacer()
}.background(Color.green)
//LOGIN BTN
Spacer()
Spacer()
Button(action: {
print("Hallo \(self.name) - dein Password ist \(self.pw)")
//NavigationDestinationLink :
})
{
HStack{
Spacer()
Text("Login")
.color(.white)
.bold()
.font(.headline)
.padding(.horizontal)
Spacer()
}
}
.background(Color.red,cornerRadius: 40)
.frame(width: 200, alignment: .bottom)
Spacer()
}.background(Color.black)
}.background(Color.yellow)
}
}
navigationView
中视图的背景颜色存在问题。它的总结是,在您要更改其背景颜色的视图之上还有另一个白色视图。所以你看不到颜色。
但是现在,您应该像这样重新排列您的视图,
1- NavigationView
应该是第一个
2- 其他所有内容(包括外部 VStack
)都应包裹在 ZStack
中
3- A Color
作为 ZStack
的第一个视图
var body: some View {
NavigationView {
ZStack {
Color.yellow
VStack{
Text("Everything else")
}
}
}
}
希望 Apple 尽快提供直接 API 这样的设置。
我的背景有问题。 自从我玩了几天 SwiftUi,一切都很好。
昨天我用背景填满了我的屏幕,但只有顶部配件不起作用,所以我撤消了我的更改。现在的问题是:我无法再访问我的背景...
其实我的后台是达不到的,所以没法改
这是我的代码:
struct ContentView : View {
@State private var name = String()
@State private var pw = String()
var body: some View {
VStack{
NavigationView {
Spacer()
//Einleitung
VStack{
Image("user")
.resizable()
.frame(width: 100 , height: 100)
Spacer()
Text("Hello User.")
Text("Please insert your Nickname and your Password")
.lineLimit(nil)
}.background(Color.blue)
.padding()
//TextField eingabe für Name + Passwort
HStack{
Spacer()
Text("Username")
.font(.headline)
Spacer()
Text("Password")
.font(.headline)
Spacer()
}.background(Color.red)
HStack{
Spacer()
TextField($name,placeholder: Text("Fill your name"))
.textFieldStyle(.roundedBorder)
.scaledToFit()
Spacer()
SecureField($pw, placeholder: Text("123456789"))
.scaledToFit()
.textFieldStyle(.roundedBorder)
Spacer()
}.background(Color.green)
//LOGIN BTN
Spacer()
Spacer()
Button(action: {
print("Hallo \(self.name) - dein Password ist \(self.pw)")
//NavigationDestinationLink :
})
{
HStack{
Spacer()
Text("Login")
.color(.white)
.bold()
.font(.headline)
.padding(.horizontal)
Spacer()
}
}
.background(Color.red,cornerRadius: 40)
.frame(width: 200, alignment: .bottom)
Spacer()
}.background(Color.black)
}.background(Color.yellow)
}
}
navigationView
但是现在,您应该像这样重新排列您的视图,
1- NavigationView
应该是第一个
2- 其他所有内容(包括外部 VStack
)都应包裹在 ZStack
3- A Color
作为 ZStack
var body: some View {
NavigationView {
ZStack {
Color.yellow
VStack{
Text("Everything else")
}
}
}
}
希望 Apple 尽快提供直接 API 这样的设置。