0

I am trying to add a search bar to my table view so it can filter settings for the user. The only problem is it is initially hidden when the view first loads, giving no indication it is available.

It only shows after you scroll up.

I found a solution that worked back in iOS 11: https://stackoverflow.com/a/46352230/7838349. Unfortunately it seems like this implementation is rather buggy in iOS 13 and seems to no longer work and still seems to show after scrolling.

For reference this is the code I have with the buggy implementation:

class SelectSettingViewController : UITableViewController {
weak var delegate: SelectSettingDelegate?
var settings: [String] = []
var filteredSettings: [String] = []

let searchController = UISearchController(searchResultsController: nil)

var isSearchBarEmpty: Bool {
  return searchController.searchBar.text?.isEmpty ?? true
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    // 1
    searchController.searchResultsUpdater = self
    // 2
    searchController.obscuresBackgroundDuringPresentation = false
    // 3
    searchController.searchBar.placeholder = "Search settings"
    // 4
    navigationItem.searchController = searchController
    // 5
    definesPresentationContext = true
    
    navigationItem.hidesSearchBarWhenScrolling = false

    
    if let indexPath = tableView.indexPathForSelectedRow {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    navigationItem.hidesSearchBarWhenScrolling = true
}
Alex
  • 620
  • 1
  • 6
  • 20

0 Answers0