JBlake
JBlake

Reputation: 1064

UISearchController Search Bar Position Drops 64 points

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.

Upvotes: 10

Views: 6733

Answers (2)

rmammadli
rmammadli

Reputation: 76

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.

Upvotes: 0

JBlake
JBlake

Reputation: 1064

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

Upvotes: 13

Related Questions