Is there a way to dismiss keyboard in SwiftUI without calling UIApplication.shared?

112 views Asked by At

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?

1

There are 1 answers

0
RoHaN On

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")
        }
    }
}