搜索栏恰好出现了 64 点,太低了:
所有其他框架都完全正确。
编辑:
- UISearchController
的 View 获取了错误的 origin.y
。当它应该为 0 时,它被设置为 64。
如果我添加这个方法:
- (void)didPresentSearchController:(UISearchController *)searchController
{
[super didPresentSearchController:searchController];
searchController.view.frame = CGRectMake(0, 0, searchController.view.frame.size.width, searchController.view.frame.size.height);
}
然后 View 对齐。但是,它很笨拙,因为它会跳跃。如果我修改 willPresentSearchController
中的框架,它将不起作用,因为 Controller 必须在其呈现后进行某种布局。
如果我使用 SparkInspector,并从原点 64 编辑 UISearchBarContainerView
的框架(它设置为 0),问题就解决了。
这是我的相关配置:
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];
我没有使用 Interface Builder,一切都在代码中配置。
我确信设置 definesPresentationContext
是正确的。
VC 位于常规 UINavigationController
中,它位于 SplitViewController 中(但 iPhone 上也存在问题)。
我觉得我缺少一个关于 UINavigationBar
我还有一个使用自定义容器 View Controller 模型的不同 Controller ,该模型更复杂,但可以工作。
当我设置
self.definesPresentationContext = NO;
所以现在 UISearchBar
得到了正确的定位,但是表示上下文是错误的,导致 UISearchController
的 TableView 占据了整个 View 。
最佳答案
以经典方式,我找到了解决方案 ( https://stackoverflow.com/a/30010473/579217 )
这就是诀窍:
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
if (bar == self.searchController.searchBar) {
return UIBarPositionTopAttached;
}
else { // Handle other cases
return UIBarPositionAny;
}
}
关于objective-c - UISearchController 搜索栏位置下降 64 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35093042/