UISearchController Search Bar Position Drops 64 points

為{幸葍}努か 提交于 2019-11-27 18:07:13

问题


The search bar is appearing exactly 64 points too low:

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:

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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/35093042/uisearchcontroller-search-bar-position-drops-64-points

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!