Fixed UISearchBar using UISearchController - Not using header view of UITableView
Asked Answered
G

3

14

Is it possible to put UISearchBar of UISearchController somewhere other than header view of UITableView?

In the apple's sample code for UISearchController, following is used.

[self.searchController.searchBar sizeToFit];
self.tableView.tableHeaderView = self.searchController.searchBar; 

Is it possible to position searchBar somewhere else? Say we want to implement a fixed UISearchBar like the one used in contacts app. I've tried this but the searchBar doesn't appear at all.

Granvillegranvillebarker answered 22/3, 2015 at 8:58 Comment(0)
A
19

You can place the UISearchBar of UISearchController in the navigation bar so that it remains fixed

self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;

// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar;

self.definesPresentationContext = YES;
Alphonso answered 22/3, 2015 at 11:41 Comment(1)
I was about to rip out the SearchController again for a manually managed UISearchBar before reading your answer, thanks for saving me a bunch of time!Champ
V
9

Create One uiview and added searchController.searchbar inside this view. After that add this view in your viewController.view. above the table view.

then it will not scroll.

   var viewTemp = UIView(frame: CGRectMake(0.0, 64.0,320.0, 44))
   viewTemp.addSubview(self.searchController.searchBar)
   self.view.addSubview(viewTemp);
Varicolored answered 19/5, 2015 at 6:46 Comment(4)
This does seem to be the solution for when you don't want to make the UISearchBar the header view of a UITableView. Thanks!Livingstone
@Livingstone In that case. just do tableView.tableHeaderView = searchController.searchBarPiggish
This works except it messes up section index scrolling :(Wish
What's the point of the temp view? Why not just add the UISearchBar directly?Tiedeman
A
0

Instead of directly keeping UISearchBar on top of UITableView. Put the UISearchBar inside a view.enter image description here

Angelinaangeline answered 22/9, 2018 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.