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?
Asked
Active
Viewed 77 times
-1
-
Do you mean to dismiss the keyboard when the view is shown? can you show related codes? – Raptor Mar 31 '23 at 03:13
1 Answers
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