gpt4 book ai didi

ios - 如何在 SwiftUI 中以编程方式编辑 TextField 的边框颜色?

转载 作者:行者123 更新时间:2023-12-04 15:13:57 25 4
gpt4 key购买 nike

这是代码片段:

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 {
...

关于ios - 如何在 SwiftUI 中以编程方式编辑 TextField 的边框颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64683613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com