user7164304
user7164304

Reputation:

disable hiding searchBar when scrolling

I am coding to setup search bar (not from storyboard)

here are my codes that related to searchbar:

    var searchController = UISearchController(searchResultsController: nil)


func setupNavBar() {
    self.navigationItem.title = "Tools"
    self.navigationController?.navigationBar.prefersLargeTitles = true
     navigationItem.hidesSearchBarWhenScrolling = false
    }


func configureSearchController() {
    filteredData = toolsList.allTools
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.searchBarStyle = .minimal
    searchController.searchBar.tintColor = .purple
    tableView.tableHeaderView = searchController.searchBar
    searchController.searchBar.placeholder = "Search for tools"
}


override func viewDidLoad() {
    super.viewDidLoad()
    setupNavBar()
    tableViewConfigurations()
    configureSearchController()
}



func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    isSearching = true
    tableView.reloadData()
}


func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    isSearching = false
    tableView.reloadData()
}

although with this line of code i ask to disable hiding search bar when scrolling, it is still hiding.

navigationItem.hidesSearchBarWhenScrolling = false

May i ask you to guide me to how i can disable it? Thank you very much

Upvotes: 1

Views: 2228

Answers (2)

user7164304
user7164304

Reputation:

@very_supercharged thanks 😊 i have transferred searchBar from table view to navigationBar i have changed:

    tableView.tableHeaderView = searchController.searchBar

to

    navigationItem.searchController = searchController

And now, searchBar does not hide when scrolling.

Upvotes: 1

klaevv
klaevv

Reputation: 467

Looking at Apple's docs I found this:

You must configure the searchController property for this property [hidesSearchBarWhenScrolling] to have any effect. The navigation controller hides and shows only the search bar provided by the search controller in that property.

I'd try adding searchController to navigationItem.

Upvotes: 1

Related Questions