Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
593 views
in Technique[技术] by (71.8m points)

objective c - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...