Siberio
Siberio

Reputation: 105

SearchBar RightButton "Cancel" disables when keyboard switches from self TextField to another TextField

I am asking my very first question here since I have still not found a solution to this weird problem. I have a search bar embedded in the navigation, and successfully managed to set the default cancel button enabled using this swift code

if let cancelButton = searchBar.value(forKey: "cancelButton") as? UIButton {
         cancelButton.isEnabled = true
    }

So that when I tap around, ending editing on search field, this button still remains active. I have put this snippet in textFieldDidEndEditing(_:) and in endEditing invoked on view tap. This search field is used to search in a table view. Some cells in the table view have a text field inside. If I tap in the search field (and the keyboard shows), then I tap in a text field in any cell, the cancel button is set disabled. Only in this scenario. What I have noticed is that the keyboard showing while editing the search field is a bit different from the one showing in the text field (the latter having the suggestion view that the first is lacking). How can I prevent the button from disabling even in this case?

EDIT
Adding a screen shot to clarify this scenario. Screenshot showing the actual screen

EDIT 2
I can't post an image directly in this post, so I will also add the sequence
Close button (on right of the search bar) is always enabled (not greyed out and tappable)

  1. Tap seach bar, so keyboard becomes visible on screen. Keyboard has not the suggestion view right on top of letters (that is actually called shortcuts bar, it is an inputAssistantItem and it is not available on iPhone's search bar)
  2. If I tap outside of the search field, keyboard is dismissed but Close button is still active and tappable, instantly removing search bar from Navigation Bar View
  3. If I tap any text field inside of any visible cell while search bar's keyboard is still visible (keyboard now has suggestion view right on top of letters, so I know this is now a text field keyboard), Close button becomes greyed out. To close search field user must now tap it twice: first time enabling it (and showing unwanted search field keyboard), second to close search field and show back Navigation Title

I am using searchBarCancelButtonClicked(_ searchBar: UISearchBar) to remove search bar from Navigation Title View. When I add it, the default Close button behaviour is to be grayed out (and not directly tappable) if search bar is not first responder (I guess), while I'd like it to be always enabled (that's why I hadded the code snippet from above).
I have used this question to help me out achieving what I wanted
How do you keep the cancel button in the search bar enabled when the keyboard is dismissed?
I have added and checked searchBarTextDidEndEditing(_ searchBar: UISearchBar), searchBarShouldEndEditing(_ searchBar: UISearchBar), and overridden resignFirstResponder(), the latter being the only one not being called. Still Close button becomes disabled switching directly from search field to text field.

Upvotes: 0

Views: 71

Answers (1)

Siberio
Siberio

Reputation: 105

I did some more tests, trying to test how search bar would respond to searchBarTextDidEndEditing(_ searchBar: UISearchBar) and searchBarShouldEndEditing(_ searchBar: UISearchBar), but the problem was in the text field becoming first responder. I knew that keyboard changing appearence (i.e. adding shortcuts bar) had something to do about it.
Since I was using a custom text field, I added an optional closure in it. That closure is called in the overridden becomeFirstResponder() text field methond and passed by the view controller that owns the table view.
View Controller sets the closure to be the same snipped posted above in the question; that snipped is then called whenever text field becomes first responder, ensuring hte button remains active.

Upvotes: 0

Related Questions