UISearchController 搜索栏位置下降 64 点
UISearchController Search Bar Position Drops 64 points
搜索栏刚好出现 64 点,太低了:
所有其他帧都完全正确。
编辑:
- UISearchController
的观点是错误的 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);
}
然后观点一致。但是,它很笨拙,因为它会跳跃。如果我修改 willPresentSearchController
中的框架,它不起作用,因为控制器必须在其呈现后进行某种布局。
如果我使用 SparkInspector,并编辑 UISearchBarContainerView
的框架,从原点 64(设置为 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 位于 SplitViewController 内部的常规 UINavigationController
中(但 iPhone 上也存在问题)。
我觉得我缺少一个关于 UINavigationBar
的简单配置选项
我还有一个使用自定义 Container View Controller 模型的不同控制器,该模型更复杂,而且可以工作。
当我设置
self.definesPresentationContext = NO;
发生这种情况:
所以现在 UISearchBar
定位正确,但是显示上下文错误,导致 UISearchController
的 table 视图占据整个视图。
嗯,按照经典的方式,我找到了解决方案 ()
这样就可以了:
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
if (bar == self.searchController.searchBar) {
return UIBarPositionTopAttached;
}
else { // Handle other cases
return UIBarPositionAny;
}
}
如果您的 tableView 是自定义 UItableView 而不是 UItableViewController 您应该隐藏顶部table 在导航栏下查看,因为搜索栏自动附加到 table 视图的 header 上,如果您的 table 视图在导航栏下启动,我会导致此问题。只需将导航栏顶部的 table 全屏显示即可。
搜索栏刚好出现 64 点,太低了:
所有其他帧都完全正确。
编辑:
- UISearchController
的观点是错误的 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);
}
然后观点一致。但是,它很笨拙,因为它会跳跃。如果我修改 willPresentSearchController
中的框架,它不起作用,因为控制器必须在其呈现后进行某种布局。
如果我使用 SparkInspector,并编辑 UISearchBarContainerView
的框架,从原点 64(设置为 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 位于 SplitViewController 内部的常规 UINavigationController
中(但 iPhone 上也存在问题)。
我觉得我缺少一个关于 UINavigationBar
我还有一个使用自定义 Container View Controller 模型的不同控制器,该模型更复杂,而且可以工作。
当我设置
self.definesPresentationContext = NO;
发生这种情况:
所以现在 UISearchBar
定位正确,但是显示上下文错误,导致 UISearchController
的 table 视图占据整个视图。
嗯,按照经典的方式,我找到了解决方案 (
这样就可以了:
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
if (bar == self.searchController.searchBar) {
return UIBarPositionTopAttached;
}
else { // Handle other cases
return UIBarPositionAny;
}
}
如果您的 tableView 是自定义 UItableView 而不是 UItableViewController 您应该隐藏顶部table 在导航栏下查看,因为搜索栏自动附加到 table 视图的 header 上,如果您的 table 视图在导航栏下启动,我会导致此问题。只需将导航栏顶部的 table 全屏显示即可。