[英]How can I programmatically edit TextField's border color in SwiftUI?
這是代碼片段:
TextField("Email", text: self.$email)
.padding()
.overlay(RoundedRectangle(cornerRadius: 1)
.stroke(Color.black, lineWidth: 1))
SecureField("Password", text: self.$password)
.padding()
.overlay(RoundedRectangle(cornerRadius: 1)
.stroke(Color.black, lineWidth: 1))
Button(action: {
print("The button was clicked!")
if loginAndPasswordAreOK() {
print("Login & Password are OK!")
} else {
self.email = ""
self.password = ""
}
}, label: {
Text("Log In")
.fontWeight(.bold)
.padding()
如果登錄名和密碼輸入不正確,如何將電子郵件的文本字段的邊框顏色更改為紅色?
您可以為此使用顯式狀態變量,例如
@State private var isValid = true
...
TextField("Email", text: self.$email)
.padding()
.overlay(RoundedRectangle(cornerRadius: 1)
.stroke(isValid ? Color.black : Color.red, lineWidth: 1))
...
Button(action: {
print("The button was clicked!")
isValid = loginAndPasswordAreOK()
if isValid {
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.