Ios Can';t选择SwiftUI文本字段
我无法在文本字段中选择和输入任何文本。如果我将它们移出当前的VStack并放入第一个VStack,它们将按预期工作。它们下面的按钮在它们所在的位置工作正常,因此文本字段似乎不在任何后面 这是我目前的布局Ios Can';t选择SwiftUI文本字段,ios,swift,swiftui,Ios,Swift,Swiftui,我无法在文本字段中选择和输入任何文本。如果我将它们移出当前的VStack并放入第一个VStack,它们将按预期工作。它们下面的按钮在它们所在的位置工作正常,因此文本字段似乎不在任何后面 这是我目前的布局 import SwiftUI struct ContentView: View { @State var emailAddress = "" @State private var password = "" @State var showingAlert = false
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
确实有些奇怪的事情发生了,但是,您可以通过在
文本字段和安全字段上使用zIndex()
来解决它:
TextField(“Email”,text:$emailAddress,onEditingChanged:{edit in},onCommit:{})
.frame(宽度:UIScreen.main.bounds.size.width-80,高度:41,对齐:。中心)
.textFieldStyle(.plain)
.background(Color.init(红色:211.0/255.0,绿色:211.0/255.0,蓝色:211.0/255.0))
.textContentType(.emailAddress)
.转弯半径(10)
.multilitextalignment(.center)
.padding(.top,15)
.padding(.bottom,10)
.foregroundColor(.blue)
.zIndex(1)
SecureField(“密码”,文本:$Password)
.frame(宽度:UIScreen.main.bounds.size.width-80,高度:41,对齐:。中心)
.textFieldStyle(.plain)
.background(Color.init(红色:211.0/255.0,绿色:211.0/255.0,蓝色:211.0/255.0))
.textContentType(.password)
.转弯半径(10)
.multilitextalignment(.center)
.foregroundColor(.blue)
.zIndex(1)
确实有些奇怪的事情发生了,但是,您可以通过在文本字段和安全字段上使用zIndex()
来解决它:
TextField(“Email”,text:$emailAddress,onEditingChanged:{edit in},onCommit:{})
.frame(宽度:UIScreen.main.bounds.size.width-80,高度:41,对齐:。中心)
.textFieldStyle(.plain)
.background(Color.init(红色:211.0/255.0,绿色:211.0/255.0,蓝色:211.0/255.0))
.textContentType(.emailAddress)
.转弯半径(10)
.multilitextalignment(.center)
.padding(.top,15)
.padding(.bottom,10)
.foregroundColor(.blue)
.zIndex(1)
SecureField(“密码”,文本:$Password)
.frame(宽度:UIScreen.main.bounds.size.width-80,高度:41,对齐:。中心)
.textFieldStyle(.plain)
.background(Color.init(红色:211.0/255.0,绿色:211.0/255.0,蓝色:211.0/255.0))
.textContentType(.password)
.转弯半径(10)
.multilitextalignment(.center)
.foregroundColor(.blue)
.zIndex(1)
您是否尝试过(a)移除外部VStack
以及(b)移除ZStack
?至少这样可以消除ZStack
作为罪魁祸首。@dfd我尝试将文本字段移动到文本标签正下方的外部VStack,它们在那里工作。我不明白为什么它们在当前的VStack中不可选择。按钮工作正常。您是否尝试过(a)移除外部VStack
以及(b)移除ZStack
?至少这样可以消除ZStack
作为罪魁祸首。@dfd我尝试将文本字段移动到文本标签正下方的外部VStack,它们在那里工作。我不明白为什么它们在当前的VStack中不可选择。按钮很好用,谢谢!这很有效。我仍然很好奇是什么原因导致没有zIndex它不能工作。是的,我不知道!看起来这些按钮不知何故占据了比它们应该占据的更多的空间,而且因为它们在层次结构中位于后面,它们无形中“阻碍”了您的文本字段。通过使用zIndex,您只需颠倒它们的顺序。但这很奇怪,因为在按钮周围绘制边框并不能显示这一点。我想我们将不得不把它留给我们仍然处于beta 4的事实。我没有尝试过,但另一个可能的解决方案可能是使用contentShape()
定义按钮的点击区域。但是,zIndex()要简单得多。我只是为了完整起见才提到它谢谢这很有效。我仍然很好奇是什么原因导致没有zIndex它不能工作。是的,我不知道!看起来这些按钮不知何故占据了比它们应该占据的更多的空间,而且因为它们在层次结构中位于后面,它们无形中“阻碍”了您的文本字段。通过使用zIndex,您只需颠倒它们的顺序。但这很奇怪,因为在按钮周围绘制边框并不能显示这一点。我想我们将不得不把它留给我们仍然处于beta 4的事实。我没有尝试过,但另一个可能的解决方案可能是使用contentShape()
定义按钮的点击区域。但是,zIndex()要简单得多。我只是为了完整起见才提到它