Search code examples
iosswiftuitableviewuisearchcontroller

swift table view is locked when searchbar active


i'm using a large navigation bar which includes a searchcontroller. If i dont search i can scroll through my tableview without problems, but if i'm searching it seams like its locked. Here is my code:

func updateSearchResults(for searchController: UISearchController) {

    // First we will check if input is only containing numbers => search for PLZ otherwise we will check if a restaurant is called like this otherwise search if there is a suitable city
    self.navigationItem.title = searchController.searchBar.text!

if !searchController.isActive{
        //TODO get all restaurants for default city
    self.navigationItem.title = "München"

    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "partnerscell", for: indexPath) as! PartnersCellTableViewCell

    return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    var bounds = UIScreen.main.bounds
    var width = bounds.size.width
    var height = bounds.size.height
    return height/2.2
}

@IBOutlet weak var tv: UITableView!
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
    super.viewDidLoad()
            searchController.searchResultsUpdater = self
 if #available(iOS 11.0, *) {
             self.navigationController?.navigationBar.prefersLargeTitles = true
    }

    self.navigationController?.navigationBar.isTranslucent = true

    self.navigationItem.searchController = searchController
    self.navigationController?.navigationBar.shadowImage = UIImage()
}

Also if i'm searching my toolbar looks darker. Please find attached two screenshots:Searchbar is not active, scrolling is possible

Searchbar is active, scrolling impossible


Solution

  • You can not access tableview when your search is active because attribute of UISearchController obscuresBackgroundDuringPresentation is default true which indicating underlying content of controller is obscured when search is active. So you can set as follow:

    override func viewDidLoad() {
        super.viewDidLoad()
                searchController.searchResultsUpdater = self
     if #available(iOS 11.0, *) {
                 self.navigationController?.navigationBar.prefersLargeTitles = true
        }
    
        self.navigationController?.navigationBar.isTranslucent = true
    
        self.navigationItem.searchController = searchController
        self.navigationController?.navigationBar.shadowImage = UIImage()
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.definesPresentationContext = true
    }