【发布时间】:2020-02-13 18:34:12
【问题描述】:
我在 SwiftUI 中构建了一个登录屏幕。当用户完成输入他们的电子邮件时,我想专注于密码SecureField。我该怎么做?
struct LoginView: View {
@State var username: String = ""
@State var password: String = ""
var body: some View {
ScrollView {
VStack {
TextField("Email", text: $username)
.padding()
.frame(width: 300)
.background(Color(UIColor.systemGray5))
.cornerRadius(5.0)
.padding(.bottom, 20)
.keyboardType(.emailAddress)
SecureField("Password", text: $password)
.padding()
.frame(width: 300)
.background(Color(UIColor.systemGray5))
.cornerRadius(5.0)
.padding(.bottom, 20)
Button(action: {
}, label: {
Text("Login")
.padding()
.frame(width: 300)
.background((username.isEmpty || password.isEmpty) ? Color.gray : Color(UIColor.cricHQOrangeColor()))
.foregroundColor(.white)
.cornerRadius(5.0)
.padding(.bottom, 20)
}).disabled(username.isEmpty || password.isEmpty)
【问题讨论】:
-
在 iOS 15 中,我们现在可以使用
@FocusState来控制应该关注哪个字段 - 请参阅 this answer。