Sunday, October 1, 2017

How to lock UISearchBar in place to superview

Leave a Comment

I am using the UISearchController alongside its UISearchBar. The UISearchController alongside its UITableView is placed inside a subview in my main view.

My goal is to lock the UISearchBar in its place so when its active, it does not move. Right now the following is happening:

enter image description here enter image description here

I have searched this issue extensively and have tried the following such as:

  1. self.searchController.hidesNavigationBarDuringPresentation = false, which just abruptly moves the search bar to the middle of the screen.

  2. definesPresentationContext = true, which does not change behavior

  3. self.extendedLayoutIncludesOpaqueBars = true, which does not change behavior

  4. Using the UIBarPositioningDelegate method mentioned here, which strangely cannot be translated to Swift because I cannot insert the AnyObject<UIBarPositioning> as it is in the Objective C version and still have it recognized as the override function

Any suggestion is appreciated.

Edit:

Here is the setup for the search items:

    searchController.searchResultsUpdater = self     searchController.dimsBackgroundDuringPresentation = false     definesPresentationContext = true     searchTableView.tableHeaderView = searchController.searchBar 

1 Answers

Answers 1

Set your TableView's Style property to Plain in the Storyboard and use the SearchBar as the section header view. Along with the following code, I commented out tableView.tableHeaderView = searchController.searchBar in Apple's "Table Search with UISearchController" example and it worked fine.

override func numberOfSections(in tableView: UITableView) -> Int {     return 1 } override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {     return searchController.searchBar.frame.size.height } override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {     return searchController.searchBar } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment