1 | initial version |
The @FocusState property wrapper is used in SwiftUI to track the focused state of a view. It can be used in conjunction with view models to store and set the focus state of a particular view. Here is the process for utilizing @FocusState with view models:
@FocusState private var isFocused: Bool
TextField("Username", text: $username)
.focused($viewModel.isFocused)
@FocusState private var isFocused: Bool
var body: some View {
TextField("Username", text: $username)
.focused($isFocused)
}
@FocusState private var isFocused: Bool
func onFocusChange() {
if isFocused {
// Perform some action when the view gains focus
} else {
// Perform some action when the view loses focus
}
}
TextField("Username", text: $username)
.onChange(of: isFocused) { isFocused in
onFocusChange()
}
.focused($isFocused)