删除表单 SwiftUI 中的行

发布于 2025-01-11 11:35:13 字数 3720 浏览 0 评论 0原文

我有一个表单,如果用户没有向电子邮件和密码字段提供足够的凭据,则会显示提示。问题是提示在它们所在的位置(在电子邮件和密码下)创建了一个额外的行。

输入图片此处的描述

我尝试隐藏分隔符,但随后留下了巨大的空间间隙。有没有办法可以删除列表行,并在文本出现时让每个元素向下移动?密码提示并不那么明显,因为按钮和行之间仍然有空间。

输入图片此处描述

NavigationView{
            ZStack{
                Form{
        Section(){
            TextField("FirstName", text: $userInformation.firstname)
            TextField("LastName", text: $userInformation.lastname)
            
            TextField("Email", text: $userInformation.email)
            Text(userInformation.emailPrompt)
                .font(.caption).italic().foregroundColor(.red)
                .listStyle(.inset)
                          
            SecureField("Passsword", text: $userInformation.password
                        ).autocapitalization(.none)
            Text(userInformation.passwordPrompt)
            
                .font(.caption).italic().foregroundColor(.red)
        }
       
                    .listRowBackground(Color.clear)
                    .padding()
                .navigationBarTitle(Text("Lets Get Started"))
                .navigationBarTitleDisplayMode(.automatic)
                .font(.system(size:18))
                    
                .listStyle(GroupedListStyle())
               
            }
               
                    NavigationLink(destination: JournalEntryMain() .navigationBarHidden(true)){
                        Text("Sign Up")
                            .frame(width: 250, height: 20)
                            .foregroundColor(.white)
                            .padding()
                           // .multilineTextAlignment(.leading)
                            .background(LinearGradient(gradient: Gradient(colors: [.orange, .pink]), startPoint: .leading, endPoint: .bottom))
                            .font(.title3)
                            .background(.clear)
                            .cornerRadius(5)
                            .offset(y:30)
                            .opacity(userInformation.isSignUpComplete ? 1 : 0.5)
                }
           
class FormViewModel: ObservableObject {
    @Published var firstname = ""
    @Published var lastname = ""
    @Published var email = ""
    @Published var gender = ""
    @Published var password = ""
    
    func isPasswordValid() -> Bool {
        let passwordTest = NSPredicate(format: "SELF MATCHES%@", "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}$")
        return passwordTest.evaluate(with: password)
    }
    func isEmailValid() -> Bool {
        let emailTest = NSPredicate(format: "SELF MATCHES%@", "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")
        return emailTest.evaluate(with: email)
    }
    
    //email and password prompts if incomplete or incorrect
    var emailPrompt: String {
        if isEmailValid() || email == "" {
            return ""
        } else {
            return "*Please enter a valid email address"
        }
    }
    
    var passwordPrompt: String {
        if isPasswordValid() || password == ""{
            return ""
        } else {
            return "*Password must be 8-15 letters, with atleast one uppercase and one number"
        }
    }
    
    var isSignUpComplete: Bool {
        if !isPasswordValid() || !isEmailValid() || firstname == "" || lastname == "" {
            return false
        }
        return true
    }
    
    
}

     

I have a form where if a user doesn't provide adaquate credentials to the email and password fields a prompt will show up. Issue is the prompts are creating an extra row where they are present (under email and password).

enter image description here

I tried hiding the separators, but then I'm left with a huge gap of space. Is there a way I can remove the list rows and just have every element shift down when the text is present like so? The password prompt isn't as noticeable as there still is space between the button and row.

enter image description here

NavigationView{
            ZStack{
                Form{
        Section(){
            TextField("FirstName", text: $userInformation.firstname)
            TextField("LastName", text: $userInformation.lastname)
            
            TextField("Email", text: $userInformation.email)
            Text(userInformation.emailPrompt)
                .font(.caption).italic().foregroundColor(.red)
                .listStyle(.inset)
                          
            SecureField("Passsword", text: $userInformation.password
                        ).autocapitalization(.none)
            Text(userInformation.passwordPrompt)
            
                .font(.caption).italic().foregroundColor(.red)
        }
       
                    .listRowBackground(Color.clear)
                    .padding()
                .navigationBarTitle(Text("Lets Get Started"))
                .navigationBarTitleDisplayMode(.automatic)
                .font(.system(size:18))
                    
                .listStyle(GroupedListStyle())
               
            }
               
                    NavigationLink(destination: JournalEntryMain() .navigationBarHidden(true)){
                        Text("Sign Up")
                            .frame(width: 250, height: 20)
                            .foregroundColor(.white)
                            .padding()
                           // .multilineTextAlignment(.leading)
                            .background(LinearGradient(gradient: Gradient(colors: [.orange, .pink]), startPoint: .leading, endPoint: .bottom))
                            .font(.title3)
                            .background(.clear)
                            .cornerRadius(5)
                            .offset(y:30)
                            .opacity(userInformation.isSignUpComplete ? 1 : 0.5)
                }
           
class FormViewModel: ObservableObject {
    @Published var firstname = ""
    @Published var lastname = ""
    @Published var email = ""
    @Published var gender = ""
    @Published var password = ""
    
    func isPasswordValid() -> Bool {
        let passwordTest = NSPredicate(format: "SELF MATCHES%@", "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,15}
quot;)
        return passwordTest.evaluate(with: password)
    }
    func isEmailValid() -> Bool {
        let emailTest = NSPredicate(format: "SELF MATCHES%@", "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)
quot;)
        return emailTest.evaluate(with: email)
    }
    
    //email and password prompts if incomplete or incorrect
    var emailPrompt: String {
        if isEmailValid() || email == "" {
            return ""
        } else {
            return "*Please enter a valid email address"
        }
    }
    
    var passwordPrompt: String {
        if isPasswordValid() || password == ""{
            return ""
        } else {
            return "*Password must be 8-15 letters, with atleast one uppercase and one number"
        }
    }
    
    var isSignUpComplete: Bool {
        if !isPasswordValid() || !isEmailValid() || firstname == "" || lastname == "" {
            return false
        }
        return true
    }
    
    
}

     

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟柳画桥 2025-01-18 11:35:13

最简单的方法是条件:

if userInformation.passwordPrompt != "" {
    Text(userInformation.passwordPrompt)
        .font(.caption).italic().foregroundColor(.red)
}

这将有条件地将其从视图中删除。您还可以设置出现/消失的动画。

The simplest approach would be a conditional:

if userInformation.passwordPrompt != "" {
    Text(userInformation.passwordPrompt)
        .font(.caption).italic().foregroundColor(.red)
}

This will conditionally remove it from the view. You can also animate the appearance/disappearance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文