swift - Swiftui 文本字段在完成时显示错误
问题描述
我正在尝试在 TextField 问题上添加验证未更改我尝试更改 bool 值以便它可以显示我的验证文本并且它也可以正常工作但问题是当我输入它时会显示错误并且当我添加 @ 错误时这是验证,但我需要在用户转到下一个字段或完成整个电子邮件后出现错误。
我的代码
struct LoginScreenView: View {
let screenWidth = UIScreen.main.bounds.size.width
let screenHeight = UIScreen.main.bounds.size.height
let screenSize = UIScreen.main.bounds.size
@State var isEmailConfig:Bool = false
@State private var givenEmail: String = ""
@State private var givenPassword: String = ""
@Environment(\.presentationMode) var presentationMode
var body: some View {
ZStack{
Color("GreyColor").ignoresSafeArea()
VStack{
HStack{
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Image(systemName: "xmark").font(.system(size: 20)).foregroundColor(.gray)
}
Spacer()
Text("Later").foregroundColor(Color.gray)
}.padding(EdgeInsets(top: screenHeight * 0.0272, leading: screenWidth * 0.06, bottom: 0, trailing: screenWidth * 0.06))
Text("Login an account")
.font(.system(size: 28))
.fontWeight(.bold)
.foregroundColor(Color.black).padding(EdgeInsets(top: screenHeight * 0.022, leading: 0, bottom: screenHeight * 0.035, trailing: 0))
TextField("Email Address", text: $givenEmail)
.foregroundColor(Color.red).offset(x:10, y: 0)
.frame(height: screenHeight * 0.0625)
.textFieldStyle(PlainTextFieldStyle())
.padding([.horizontal], 4)
.onChange(of: givenEmail, perform: { value in
print(givenEmail)
if(givenEmail == "" || givenEmail.contains("@")){
self.isEmailConfig = false;
print("asd")
}
else{
self.isEmailConfig = true;
print("asdsad")
}
})
.disableAutocorrection(true)
.autocapitalization(.none)
.background(RoundedRectangle(cornerRadius: 10).fill(Color.white))
.padding(EdgeInsets(top: 0, leading: screenWidth * 0.06, bottom: screenHeight * 0.01, trailing: screenWidth * 0.06))
if self.isEmailConfig {
Text("Email is not valid")
}
}
}
}
}
解决方案
推荐阅读
- python-3.x - 如何使用python在每行文件中的值分隔符之前和之后修剪前导和尾随空格
- python - 如何在 Python 中与线程并行运行两个函数?
- powershell - 如何向查询添加另一个查询
- django - 相当于 reverse() 的 Django 通道
- image - 使用 jQuery 上传前预览图像
- kotlin - Kotlin - 检查参数是否为空的通用方法
- mysql - 在 laravel 中显示带有 where not in 子句的值
- javascript - 在 html 画布中单击时停止动画播放
- python - 将俄语文本翻译成我可以放在 python 链接中的文本
- javascript - 将 JSON 数据绑定到 HTML 元素