5

I have a UITableView which doesn't reach the entire view. Approximatively, the UITableView is two times smaller than the view.

Then, I add "Search Bar and Search Display Controller" to the UITableView from the Object library. So, to visualize, the searchBar appears at the screen's center.

Here's my view :

enter image description here

My problem : When I click into the search field to do my research, the searchBar is going to the top of the view, in place of the navigation bar ! And the transition is very awful... How can I constraint the searchBar to stay in its initial place ?

Jonathan F.
  • 2,357
  • 4
  • 26
  • 44
  • Have the same exact problem and I was going to ask for it toady. The search bar is intended to move to the top but the problem is the empty space it leaves on the table. I believe since the controller's view is not solely a table view then the search bar cannot manipulate the table view directly to move up and cover the empty space – giorashc Dec 10 '13 at 12:34
  • @giorashc have you set frame for self.searchDisplayController or self.searchDisplayController.searchResultsTableView. Try setting frame for it.. and let me know – – the1pawan Dec 10 '13 at 12:45
  • Use simple UISearchBar instead of UISearchDisplayController – iEinstein Dec 10 '13 at 13:02
  • @the1pawan I tried moving the table view by modifying its frame but it didn't work. In my case I do not care that the search bar is on top but the empty space it leaves in the table view is my problem – giorashc Dec 10 '13 at 13:09
  • @AshutoshMishra The problem does not appear when using only UISearchBar. But without using UISearchDisplayController, you can't access to delegate methods like **shouldReloadTableForSearchString** or **shouldReloadTableForSearchScope**... – Jonathan F. Dec 10 '13 at 13:19
  • @Erzékiel- you can make your custom own too – iEinstein Dec 10 '13 at 13:21
  • @Erzékiel you do not need to force those methods, there are alternatives – Ilario Dec 10 '13 at 13:25
  • OK, anyway, I would like to keep the Search Display Controller in place. Why the searchBar is going on top and leave an unsightly blank ? – Jonathan F. Dec 10 '13 at 13:45
  • please look at this link, it seems same issue. http://stackoverflow.com/a/18988959/2482283 – andrewchan2022 Feb 17 '14 at 15:05

1 Answers1

5

The problem is related to the search display controller. You don't have the control over the animation and it doesn't seem to handle it well when there's a view sitting on top of your TableView. We had the same exact problem and the easiest way we fixed it is by replacing the Search Display Controller by a simple Search Bar in the Storyboard.

That means your class will now have to implement the delegate method of the search bar to work properly.

To animate properly, simply override the searchBarTextDidBeginEditing and searchBarTextDidEndEditing like so:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.origin.y = tableViewFrame.origin.y - self.yourHeaderView.bounds.size.height;
[UIView animateWithDuration:0.25
                 animations:^{ self.tableView.frame = tableViewFrame; }];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.origin.y = tableViewFrame.origin.y + self.yourHeaderView.bounds.size.height;
[UIView animateWithDuration:0.25
                 animations:^{ self.tableView.frame = tableViewFrame; }];
}

Simply put, you just move the table view frame over the view based on it's height. The animation makes it smoother and cleaner.

Gabriel Cartier
  • 1,734
  • 20
  • 22