9

The search bar is appearing exactly 64 points too low:

enter image description here

All of the other frames are exactly correct.

Edit: - It's the UISearchController's view that is getting the wrong origin.y. It gets set to 64, when it should be 0. If I add this method:

- (void)didPresentSearchController:(UISearchController *)searchController
{
  [super didPresentSearchController:searchController];
  searchController.view.frame = CGRectMake(0, 0, searchController.view.frame.size.width, searchController.view.frame.size.height);

}

Then the views align. However, its janky because it jumps. If I modify the frame in willPresentSearchController it does not work, as the controller must be doing some sort of layout after its presented.

If I use SparkInspector, and edit the frame of the UISearchBarContainerView from origin 64 (what it gets set at, to 0), the problem is resolved.

Here is my relevant configuration:

self.searchResultsController = [[GMSearchTableViewController alloc] init];
self.definesPresentationContext = YES;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsController];
self.searchController.dimsBackgroundDuringPresentation = YES;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
[self.view addSubview:self.searchController.searchBar];

I'm not using Interface Builder, everything is configured in code. I'm positive that setting definesPresentationContext is correct.

The VC sits in a regular UINavigationController, which is inside a SplitViewController (but problem exists on iPhone as well).

I feel like I'm missing a simple configuration option in regards to the UINavigationBar

I also have a different controller that uses a custom Container View Controller model, which is more complex, and that one works.

When I set

self.definesPresentationContext = NO;

This happens: enter image description here

So now the UISearchBar gets positioned correctly, but the presentation context is wrong, causing the UISearchController's table view to occupy the full view.

Cœur
  • 32,421
  • 21
  • 173
  • 232
JBlake
  • 926
  • 11
  • 26
  • Thank You for giving me solution in your question. It helped. self.definesPresentationContext = NO; – u_gandhi Aug 18 '17 at 12:20

2 Answers2

13

Well in classic fashion, I've found a solution (https://stackoverflow.com/a/30010473/579217)

This does the trick:

- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
  if (bar == self.searchController.searchBar) {
    return UIBarPositionTopAttached;
  }
  else { // Handle other cases
    return UIBarPositionAny;
  }
}
Community
  • 1
  • 1
JBlake
  • 926
  • 11
  • 26
0

If your tableView is custom UItableView and not a UItableViewController you should hide top of the tableview under navigation bar, because search bar automatically attached on header of table view and if your table view starts under navigation bar i cause this problem. Just make the table view full screen from top of navigation bar.

rmammadli
  • 76
  • 1
  • 7
  • My table view is custom, however I attach the search bar manually using the tableview's contentInsets instead of the header view, because I want it always visible. I had the search controller hide the navigation bar before, but it looked janky and I personally like having the navigation bar still visible so the user can quickly access other actions while still searching. – JBlake Jan 30 '16 at 20:36
  • So, if the tableview is custom just add search bar before the tableview not inside it. then add table view under search bar and hide header of the table view under search bar. it will be as how you wish it.:) – rmammadli Jan 30 '16 at 21:00