I have a view with Textfields and I am using FocusStates.
My problem is that when I want to show an error message, the Keyboard is showing up. How can I hide the keyboard when the view is reloading?
struct SignInUI: View {
@State var email: String = ""
@State var password: String = ""
private enum Field: Int, CaseIterable {
case username, password
}
@FocusState private var focusedField: Field?
@StateObject var viewModel: SignInViewModel
var body: some View {
Group {
switch viewModel.state {
case .loading:
ZStack {
content
ProgressView()
}
case .error:
ZStack {
content
ErrorView()
}
case .loaded:
content
}
}
}
var content: some View {
VStack {
PasswordTextField(field: $password)
.focused($focusedField, equals: .password)
EmailTextField(field: $email)
.focused($focusedField, equals: .username)
ButtonView(text: "Sign In", action: {
focusedField = nil
viewModel.signIn(email: email, password: password)
})
}
}
}