I am trying to achieve hiding keyboard when .onTap anywhere in screen by using new .focused() methods which came with iOS15 (SwiftUI 3).
First declare my FocusedValueKey
struct FocusedTextField: FocusedValueKey {
enum Field: Hashable {
case firstName
case lastName
case emailAddress
}
typealias Value = FocusState<Field>.Binding
}
extension FocusedValues {
var textField: FocusedTextField.Value? {
get { self[FocusedTextField.self] }
set { self[FocusedTextField.self] = newValue }
}
}
then in MainApp, I put FocusState to pass nil to the path when I tapped anywhere in screen.
struct TextFieldTestApp: App {
@FocusState private var state: FocusedTextField.Field?
var body: some Scene {
WindowGroup {
ContentView()
.onTapGesture { state = nil }
.focusedValue(\.textField, $state)
}
}
}
Then I create my ContentView
struct ContentView: View {
@State private var firstName = ""
@State private var lastName = ""
@State private var emailAddress = ""
// Error: Key path value type 'FocusedTextField.Value?' (aka 'Optional<FocusState<FocusedTextField.Field>.Binding>')
// cannot be converted to contextual type 'Binding<Value>?'
@FocusedBinding(\.textField) var focusedTextField
var body: some View {
ZStack {
Color.red
VStack {
Spacer()
TextField("Enter your first name", text: $firstName)
// Error: Type 'Hashable' has no member 'firstName'
.focused($focusedTextField, equals: .firstName)
.textContentType(.givenName)
.submitLabel(.next)
TextField("Enter your last name", text: $lastName)
// Error: Type 'Hashable' has no member 'lastName'
.focused(focusedTextField, equals: .lastName)
.textContentType(.familyName)
.submitLabel(.next)
TextField("Enter your email address", text: $emailAddress)
// Error: Type 'Hashable' has no member 'emailAddress'
.focused(focusedTextField, equals: .emailAddress)
.textContentType(.emailAddress)
.submitLabel(.join)
Spacer()
}
}
}
}
I also tried it with environment variables but @FocusState PropertyWrapper causes problems again.
