MTRL
MTRL

Reputation: 143

Fixed UISearchBar using UISearchController - Not using header view of UITableView

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.

Upvotes: 14

Views: 9650

Answers (3)

Saurabh Padwekar
Saurabh Padwekar

Reputation: 4074

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

Upvotes: 0

anil
anil

Reputation: 121

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);

Upvotes: 9

Praveen Gowda I V
Praveen Gowda I V

Reputation: 9637

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;

Upvotes: 19

Related Questions