1

I have been wondering how to implement the best practice for this kind of situation.

I have an UITableViewController, and want to add the searchBar programmatically, So I did this on my viewForHeaderInSection

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 44)];
searchBar.placeholder = @"Search";
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchBar.delegate=self;
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self; //This one keep messing with the layout
[view setBackgroundColor:[UIColor blackColor]];
[view addSubview:searchBar];
return view;
}

The commented line causing my searchBar weird position and behaviour when editing as you can see on the image below.

Normal position

When firstResponder

first responder made the status bar overlap, though I have add on my viewDidLoad

self.edgesForExtendedLayout = UIRectEdgeNone;

And when editing it causing this : Doubled Search Bar When Editing

If i remove the searchDisplayController.searchResultsDelegate = self; all the view looks perfect, but I need this delegate to fire the didSelectedRow on the searchResultTableView.. How can this possible? what is the best approach for this kind of situation. Thank you!

UPDATE

As I have searching here and there, and with the help of the answer given, I can conclude that If you want A fix SearchBar on UITableView, just create the tableview on UIViewController, put the searchBar, put some constraint and then voila!

xeravim
  • 463
  • 7
  • 20
  • I provided an answer but I just realized you're coding in objective-c and not Swift. My apologies. It should be pretty easy to convert to objective-c though. It's the exact same process, just slightly different syntax. – user3353890 May 21 '16 at 08:55
  • I added a link to my answer that will show you how to do this in objective-c. good luck! – user3353890 May 21 '16 at 08:59

1 Answers1

1

Don't create your searchController in viewForHeaderInSection, instead create a searchController variable for your viewController like so:

let searchController = UISearchController(searchResultsController: nil)

Then configure it using this function and call this function in viewDidLoad:

func configureSearchController() {
    searchController.searchBar.delegate = self
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.barTintColor = UIColor.whiteColor()
    searchController.searchBar.tintColor = UIColor.blackColor()
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar
}

Objective-C

Here is a great link for how to accomplish this in objective-c: How to implement UISearchController with objective c

Community
  • 1
  • 1
user3353890
  • 1,844
  • 1
  • 16
  • 31
  • Hi, Thank you for the answer, that is true that I should not put them on viewForHeaderInSection since I have tried put them like you said above and its perfect, but I want the search bar to float like section, so that it wont scrolled up. tableView.tableHeaderView seems cannot give what i want. Do you have any suggestion for this? – xeravim May 21 '16 at 09:15
  • Ah, so you want it floating. The best thing to do would be to use a viewController instead of a tableViewController. Put a tableView in the viewController and a searchBar on top of the tableView. I could take the time to write it out, but with a quick google search I came across this stack overflow answer that should explain how to accomplish what you want to accomplish: http://stackoverflow.com/questions/24796274/ios-fix-search-bar-on-top-of-the-uitableviewcontroller – user3353890 May 21 '16 at 09:24
  • Yeah, I guess there is no other option. Thanks man! I will accept your answer. – xeravim May 21 '16 at 09:25
  • You're welcome! It's not terribly difficult to implement and should get the desired result that you want. Just don't forget to hook up your tableView delegate and datasource when you add it to the viewController! – user3353890 May 21 '16 at 09:28
  • 1
    yeah, everything now perfect! Thank you! – xeravim May 21 '16 at 09:39
  • you're welcome! Good job! glad you got it working :) – user3353890 May 21 '16 at 09:40