0

How to set SearchBar to be active after navigating back from another view controller?

Type the search string and click search button on search suggestion page, then this page popped by navigation controller. The search result page which was navigated back has a search bar with search text, and refresh the result table view.

But this SearchBar's cancel button is inactive, the first click make search bar become editing mode and the second click just can make it work.

How to set SearchBar's cancel button to be active, so I only need to click cancel button once.

Search suggestion page, will be popped

Search result page, cancel button, two states: active and inactive

  • There is another issue that I dont want to make my search bar become editing mode to make cancel button clickable. Because the design is when search bar become editing mode on result page, the app changes to suggestion page. – Amrzs Zhang Sep 18 '17 at 18:35

1 Answers1

0

If you're just trying to activate the search bar when it's parent view appears, then you can just add the following to the view controller that contains it:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    if searchBar.canBecomeFirstResponder {
        searchBar.becomeFirstResponder()
    }
}
nicksweet
  • 3,929
  • 1
  • 20
  • 22
  • Thanks for your answer, the question is that I dont want to make my search bar become editing mode. Because the design is when search bar become editing mode on result page, the app changes to suggestion page. – Amrzs Zhang Sep 18 '17 at 18:34