-1

I have a SwiftUI view in an application extension (in which there's no access to UIApplication). Is there a way to dismiss the keyboard in this view?

Fitzgerald Afful
  • 123
  • 1
  • 15

1 Answers1

1

You can use @FocusState property wrapper. But that's available on iOS 15+

https://developer.apple.com/documentation/swiftui/focusstate

@State var textFieldText : String = ""
@FocusState private var isFocused : Bool
var body: some View {
    VStack {
        TextField("Enter some text", text: $textFieldText)
            .focused($isFocused)
        Button {
            isFocused = false // This will dismiss keyboard
        } label : {
            Text("Dismiss Keyboard")
        }
    }
}
RoHaN
  • 372
  • 3
  • 14