skip to Main Content

I am using https://www.raywenderlich.com/4363809-uisearchcontroller-tutorial-getting-started as based project.

To make UISearchController hidden initially during app startup, I have move the following line of code, from viewDidLoad to viewWillAppear.

navigationItem.searchController = searchController

In order to start search feature, user will need to

  1. "Pull down" UITableView to make UISearchController visible.
  2. Tab on the UISearchController‘s search bar so that the search bar is in focus.

Here’s how it looks like

enter image description here

However, from UX point of view, user (Especially first time iPhone user) cannot discover such search feature easily.

We also do NOT want to display the search bar when the screen shows for the first time. It occupies screen space, and search feature is not a frequent accessed feature.

To make such search feature easy discoverable, we would like to provide a bar button item (with magnifying-glass icon) at top right. If user taps on it, the app will perform step 1 and step 2 programmatically.

May I know, how can I show, then focus UISearchController programmatically?

2

Answers


  1. if I catch it correctly what did you say:

        if #available(iOS 13.0, *) {
          DispatchQueue.main.async {
            self.searchController.searchBar.searchTextField.becomeFirstResponder()
          }
          
        } else {
          // Fallback on earlier versions
        }
    
    Login or Signup to reply.
  2. You can try manually updating UISearchController.isActive to force launch the search interface.

    When the user taps in the search field of a managed search bar, the search controller automatically displays the search results controller. Usually, you get the value of this property to determine whether the search results are displayed. However, you can set this property to true to force the search interface to appear, even if the user has not tapped in the search field.
    The default value of this property is false.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search