I made a custom search, which is in the toolbar, at the .principal position (using iOS 16).
func navigationBarWithSearch(searchText: Binding<String>, keyboardAppearanceDelay: Double = 0.0, cancelAction: @escaping () -> Void) -> some View {
toolbar {
ToolbarItem(placement: .principal) {
ToolbarSearchView(
keyboardAppearanceDelay: keyboardAppearanceDelay,
text: searchText,
cancelAction: cancelAction
)
}
}
}
The textField itself is UIViewRepresentable. And everything works fine, except for one thing. It has to do with the fact that when the keyboard is shown on the screen and I do a BackSwipe to the previous screen, the keyboard doesn’t disappear (as it should).
I debugged the appearance of the keyboard and saw that when I do BackSwipe, NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification) is triggered. And then, the textFieldDidBeginEditing method is abruptly triggered (and because of that, the keyboard doesn’t disappear until I close the screen all the way).
I thought that maybe the becomeFirstResponder() method was being triggered again, but no.
I also tried broadcasting a method to force hiding the keyboard, but that didn’t help either.
Also, later on, I decided to test the operation of this textField outside the NavigationBar, and the keyboard worked fine, just as expected. It disappeared when using BackSwipe.
As far as I understand, the problem lies in the use of textField in NavigationBar, but how to solve it ?
You can see here that the keyboard twitches during BackSwipe – that’s it disappearing and reappearing. And you can see that when I pull the screen, the keyboard stays in place and does not hide.
I tried to solve the problem, debugging the keyboard appearing and disappearing, trying to use methods to make the keyboard disappear forcibly, but nothing worked. I checked textField operation separately from NavigationBar (and found out that such strange keyboard behavior only happens when textField is in NavigationBar).
I expect the keyboard to fade smoothly when using BackSwipe, as it does if I use other textFields outside the NavigationBar
2