Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar presented by UISearchController in table header view animates too far when active

I am using UISearchController to present a search bar inside the header view of a tableview:

...
self.searchController.hidesNavigationBarDuringPresentation = NO;            
self.presentingTVC.tableView.tableHeaderView = self.searchController.searchBar;
[self.searchController.searchBar sizeToFit];
self.presentingTVC.tableView.tableHeaderView = self.searchController.searchBar;

(where setting the tableHeaderView property twice is necessary as otherwise the header view overlaps the first row, c.f.a couple of answers on S.O. )

This is how it looks, perfectly in position when inactive: Inactive search bar in correct position

The search bar should just stay in place when active - I don't want it to move up to hide the navigation bar. But it unexpectedly animates down, leaving a blank space between it and the navigation bar:

Screenshot showing search bar animated too far down

Here's a video of weird search bar animation

If I just use a search bar separately from UISearchController, it does not show the same behaviour when it becomes active.

In my presenting view controller, I have self.definesPresentationContext = YES; and self.navigationController.navigationBar.translucent = YES;, and in IB none of the "extend edges" boxes are active (all seemed to be possible things that could throw search presentation off, from reading around).

Does anyone know how I can stop the search bar from animating down?

like image 263
Zoë Smith Avatar asked Feb 04 '15 16:02

Zoë Smith


2 Answers

Old question but I was able to solve this issue by setting

self.extendedLayoutIncludesOpaqueBars = YES;

on my view controller. I think is issue is caused by having an opaque navigation bar but setting hidesNavigationBarDuringPresentation to NO on your search controller, causing the search bar to incorrectly position itself when focused.

like image 166
ppilone Avatar answered Nov 16 '22 21:11

ppilone


Ok so I finally figured out a solution for myself. Though it's more than likely we have other things in our code / storyboard (that's why this is a hard question to answer), for the most part I followed Apple's tutorial on UISearchController: (https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html)

Just about all my code is identical to theirs, but I couldn't get the search bar to not jump when clicking inside of it. So what I did was check "under opaque bars" for both the original table view and the search results table view in my storyboard. That got the search bar to stop jumping.

But there was one last issue, which was the fact that the first row of the results table view was hidden by the search bar. In order to fix that I added self.tableView.contentInset = UIEdgeInsetsMake(kHeightOfTableViewCells, 0, 0, 0); to the viewDidLoad method of my results table view. Voila!

like image 30
Brian Sachetta Avatar answered Nov 16 '22 19:11

Brian Sachetta