首页 > 解决方案 > 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")
                }
                
                
            }
        }
    }
}




 

在此处输入图像描述

标签: swiftxcodeswiftui

解决方案


推荐阅读