删除表单 SwiftUI 中的行
我有一个表单,如果用户没有向电子邮件和密码字段提供足够的凭据,则会显示提示。问题是提示在它们所在的位置(在电子邮件和密码下)创建了一个额外的行。
我尝试隐藏分隔符,但随后留下了巨大的空间间隙。有没有办法可以删除列表行,并在文本出现时让每个元素向下移动?密码提示并不那么明显,因为按钮和行之间仍然有空间。
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).
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是条件:
这将有条件地将其从视图中删除。您还可以设置出现/消失的动画。
The simplest approach would be a conditional:
This will conditionally remove it from the view. You can also animate the appearance/disappearance.