clomas
clomas

Reputation: 103

Swift: searchBar still visible with navigationItem.hidesSearchBarWhenScrolling = true

In Swift I'm trying to achieve a hidden searchBar, which becomes visible when scrolling,

I'm using this

navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true

I've tried moving the above code around, but regardless the searchBar is visible no matter where this is called in the ViewController file.

is there something that could be overriding this functionality? the only other code in the file where I reference the searchBar or searchController is the following:

if self.searchController.isActive && searchController.searchBar.text != "" {...

self.searchController.searchBar.placeholder = "Search \(title)"

func updateSearchResults(for searchController: UISearchController) {

if let searchText = searchController.searchBar.text, !searchText.isEmpty {
        filteredArr = defaultArr.filter {...

func configureSearchController () {

    //Setup the Search Controller
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = true
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.searchBarStyle = .minimal
    searchController.definesPresentationContext = true
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = true 
}

// I call reloadData a few times throughout as well
tableView.reloadData()

I'm not sure how any of the above could effect hideSearchBarWhenScrolling = true.

enter image description here

I'm so confused how this is happening, does anyone have any advice?

Any help would be much appreciated.

Upvotes: 1

Views: 1455

Answers (1)

clomas
clomas

Reputation: 103

A label which was "behind" the tableView was causing the issue:

searchBar always visible:

enter image description here

searchBar hidden as intended:

enter image description here

Upvotes: 6

Related Questions