gpt4 book ai didi

ios - 无法选择 SwiftUI TextField

转载 作者:行者123 更新时间:2023-11-28 11:29:45 25 4
gpt4 key购买 nike

我无法选择任何文本并将其输入到我的文本字段中。如果我将它们移出当前的 VStack 并将它们放入第一个 VStack,它们将按预期工作。它们下方的按钮在它们所在的位置工作得很好,因此看起来文本字段似乎不在任何东西后面。

这是我当前的布局。

import SwiftUI

struct ContentView: View {
@State var emailAddress = ""
@State private var password = ""
@State var showingAlert = false
@State var error = ""
@State var showCollections = false

var body: some View {
ZStack(alignment: .top) {
LinearGradient(gradient: Gradient(colors: [.blue, .white,]), startPoint: .bottom, endPoint: .top)
.edgesIgnoringSafeArea(.all)

VStack(alignment: .leading) {
Text("Title")
.font(.largeTitle)
.frame(width: UIScreen.main.bounds.size.width - 40, height: 100, alignment: .center)
.foregroundColor(.blue)

ZStack(alignment: .top) {
Color.white
VStack(alignment: .leading) {

TextField("Email", text: $emailAddress, onEditingChanged: { edit in

}, onCommit: {

})
.frame(width: UIScreen.main.bounds.size.width - 80, height: 41, alignment: .center)
.textFieldStyle(.plain)
.background(Color.init(red: 211.0/255.0, green: 211.0/255.0, blue: 211.0/255.0))
.textContentType(.emailAddress)
.cornerRadius(10)
.multilineTextAlignment(.center)
.padding(.top, 15)
.padding(.bottom, 10)
.foregroundColor(.blue)

SecureField("Password", text: $password)
.frame(width: UIScreen.main.bounds.size.width - 80, height: 41, alignment: .center)
.textFieldStyle(.plain)
.background(Color.init(red: 211.0/255.0, green: 211.0/255.0, blue: 211.0/255.0))
.textContentType(.password)
.cornerRadius(10)
.multilineTextAlignment(.center)
.foregroundColor(.blue)

Button(action: signIn) {
Text("Sign In")
.foregroundColor(.white)
}
.frame(width: UIScreen.main.bounds.size.width - 80, height: 50, alignment: .center)
.background(Color.blue)
.cornerRadius(20, antialiased: true)

Button(action: createAccount) {
Text("Don't have an account? Create one.")
.foregroundColor(.blue)
}
.frame(width: UIScreen.main.bounds.size.width - 80, height: 50, alignment: .center)

Button(action: forgotPassword) {
Text("Forgot password?")
.foregroundColor(.blue)
}
.frame(width: UIScreen.main.bounds.size.width - 80, height: 50, alignment: .center)
}
}
.frame(width: UIScreen.main.bounds.size.width - 40, height: 250, alignment: .center)
.cornerRadius(20)
}
}
}

func signIn() {

}

func createAccount() {

}

func forgotPassword() {

}
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif

enter image description here

最佳答案

肯定会发生一些奇怪的事情,但是,您可以通过在 TextFieldSecureField 上使用 zIndex() 来解决它:

TextField("Email", text: $emailAddress, onEditingChanged: { edit in }, onCommit: { })
.frame(width: UIScreen.main.bounds.size.width - 80, height: 41, alignment: .center)
.textFieldStyle(.plain)
.background(Color.init(red: 211.0/255.0, green: 211.0/255.0, blue: 211.0/255.0))
.textContentType(.emailAddress)
.cornerRadius(10)
.multilineTextAlignment(.center)
.padding(.top, 15)
.padding(.bottom, 10)
.foregroundColor(.blue)
.zIndex(1)

SecureField("Password", text: $password)
.frame(width: UIScreen.main.bounds.size.width - 80, height: 41, alignment: .center)
.textFieldStyle(.plain)
.background(Color.init(red: 211.0/255.0, green: 211.0/255.0, blue: 211.0/255.0))
.textContentType(.password)
.cornerRadius(10)
.multilineTextAlignment(.center)
.foregroundColor(.blue)
.zIndex(1)

关于ios - 无法选择 SwiftUI TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57201866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com