Search code examples
swiftipaduisplitviewcontrollerinputaccessoryview

inputAccessoryView getting hidden on UISearchBar activation in UISplitViewController


I'm facing an issue - when UISearchBar is activated then inputAccessoryView gets hidden and doesn't show up even after de-activation of UISearchBar searching in iPad.

Video

InputAccessoryView_Issue

Steps to the issue

  1. Start UISearchbar editing
  2. Cancel UISearchBar editing
  3. Observe the inputAccessoryView visibility before and after editing

Storyboard

Storyboard

Please find the code in this repo - InputAccessoryView_Issue and feel free to commit your solution in the repo itself or here in Stackoverflow.

TIA


Solution

  • The solution that I found was to make the detail controller as first responder immediately after Searchbar ends editing. Below is the sample code for the same:

    extension NSNotification.Name {
         static let SearchbarEndEditing = NSNotification.Name("SEARCHBAR_END_EDITING")
    }
    
    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        NotificationCenter.default.post(name: .SearchbarEndEditing, object: nil)
    }
    

    And in DetailViewController:

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(handleSearchbarEndEditing),
                                               name: .SearchbarEndEditing,
                                               object: nil)
    }
    
    @objc private func handleSearchbarEndEditing(_ sender: NSNotification) {
        becomeFirstResponder()
        view.becomeFirstResponder()
    }
    

    It worked for me and hopefully, it works for everyone else as well!