Search code examples
uitableviewswift4uisearchcontroller

How to make tableview unobscured when there is text in the search bar in swift4


I have search controller that is in a tableview controller and the code is the following

let searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    super.viewDidLoad()

    ///// color of letter in search index ////
    self.tableView.sectionIndexColor = UIColor.black
    self.searchController.searchBar.delegate = self
    //////////// search Controller //////////////
    self.searchController.searchResultsUpdater = self
    //self.searchController.obscuresBackgroundDuringPresentation = true
    self.searchController.searchBar.placeholder = "Search Countries"
    self.navigationItem.searchController = searchController
    definesPresentationContext = true
    self.searchController.obscuresBackgroundDuringPresentation = true
    self.navigationItem.hidesSearchBarWhenScrolling = false



}

When I search the search bar the items in the background (list of countries) are inactive as shown in the image and that is fine enter image description here

What I want to do is make the items in background active once there is text in the search bar. I tried the following code but it does not work

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
 {
        if (searchText.count>0)
        {
            self.searchController.obscuresBackgroundDuringPresentation = false
        }
 }

My view controller conforms to UISearchBarDelegate and UISearchResultsUpdating for the info


Solution

  • Apple doc:

    This property(obscuresBackgroundDuringPresentation) controls only whether the original view controller is initially obscured.

    Apple's suggestion:

    If you use the same view controller to display the searchable content and search results, it is recommended that you set this property to false. The default value of this property is true.

    you may use tableview's isUserInteractionEnabled property instead, make it enabled after user input something.