Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI dismiss keyboard when tapping segmentedControl

I have a TextField in SwiftUI that needs to use a different keyboard depending on the value of a @State variable determined by a SegementedControl() picker.

How can I dismiss the keyboard (like send an endEditing event) when the user taps a different segment? I need to do this because I want to change the keyboard type and if the textField is the responder, the keyboard won't change.

I have this extension:

extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

And I can do something like

UIApplication.shared.endEditing()

But I don't know where or how to call this when the user taps a different segment.

I have tried putting a tapGesture on the Picker and the keyboard does dismiss, but the tap does not pass through to the picker so it does not change.

Code snippet here:

@State private var type:String = "name"

. . .

Form {
    Section(header: Text("Search Type")) {
        Picker("", selection: $type) {
            Text("By Name").tag("name")
            Text("By AppId").tag("id")
        }.pickerStyle(SegmentedPickerStyle())
    }

    Section(header: Text("Enter search value")) {
        TextField(self.searchPlaceHolder, text: $searchValue)
            .keyboardType(self.type == "name" ? UIKeyboardType.alphabet : UIKeyboardType.numberPad)
    }
}
like image 420
Stewart Lynch Avatar asked Feb 03 '23 17:02

Stewart Lynch


2 Answers

Attach a custom Binding to the Picker that calls endEditing() whenever it is set:

Section(header: Text("Search Type")) {
    Picker("", selection: Binding(get: {
        self.type
    }, set: { (res) in
        self.type = res
        UIApplication.shared.endEditing()
    })) {
        Text("By Name").tag("name")
        Text("By AppId").tag("id")
    }.pickerStyle(SegmentedPickerStyle())
}
like image 66
RPatel99 Avatar answered Feb 06 '23 07:02

RPatel99


Update since iOS 13 / iPadOS 13 was released.

Since there is now support for multiple windows in one app you need to loop through the UIWindows and end editing one-by-one.

UIApplication.shared.windows.forEach { $0.endEditing(false) }
like image 27
lawrencebensaid Avatar answered Feb 06 '23 09:02

lawrencebensaid