UISearchController:UITableView的Section索引与searchBar重叠

Bri*_*tta 7 uitableview uisearchbar ios uisearchcontroller

我正在构建一个使用新UISearchController的iOS 8应用程序.在与搜索控制器相关的表视图中,我使用区段索引让用户从表的一个部分快速跳转到下一个部分.它工作正常,但部分索引与表/搜索控制器上的搜索栏重叠.有没有人遇到过这个问题,如果有的话,你是怎么解决的?以下是我正在初始化搜索控制器的方法:

self.resultsTableController = [self.storyboard instantiateViewControllerWithIdentifier:[SelectSpecialtySearchResultsTVC storyboardId]];
UINavigationController *searchResultsNavController = [[UINavigationController alloc]initWithRootViewController:self.resultsTableController];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsNavController];

self.searchController.searchResultsUpdater = self;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.barTintColor = [UIColor colorWithHexString:kColorGrayLight];
self.searchController.searchBar.translucent = NO;
self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, [self.view bounds].size.width, 44.0);
self.searchController.searchBar.delegate = self;

self.tableView.tableHeaderView = self.searchController.searchBar;

//present the search display controller within the confines of this class's table view
self.definesPresentationContext = YES;

// we want to be the delegate for our filtered table so didSelectRowAtIndexPath is called for both tables
self.resultsTableController.tableView.delegate = self;
self.searchController.delegate = self;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

rgr*_*eso 11

只需更改sectionIndex的背景颜色:

迅速:

    tableView.sectionIndexBackgroundColor = UIColor.clearColor()
Run Code Online (Sandbox Code Playgroud)


Bri*_*tta 1

最后针对这个问题提出了一个可以接受的解决方案。这个答案是基于我上面的问题(我使用所有相同的代码减去此处更改的代码)。这里要注意的主要事情是 @andrewbuilder 是对的 - 将搜索栏添加为表头视图将保证搜索栏将滚动移动;它的行为就像任何其他表头视图一样。

我最终所做的是一些事情的结合。我在方法中所做的主要改变是改变主控制器呈现搜索控制器的方式。具体来说,我从self.definesPresentationContext = YES;self.definesPresentationContext = NO;

其作用本质上是将您的搜索控制器显示在主屏幕顶部而不是主屏幕内。这会产生一种设计权衡,因为您现在必须处理 2 个视图控制器的布局,而不仅仅是一个。但这是我可以接受的。

我做的下一件事是UISearchBar在我的主视图控制器中创建一个指向 a 的强指针,我将其称为searchBar. 然后,我将该属性设置为等于我的搜索控制器的 searchBar 并将其添加到我的子视图中,如下所示:

self.searchBar = self.searchController.searchBar;
[self.view addSubview:self.searchBar];
Run Code Online (Sandbox Code Playgroud)

为了使其看起来正确,我必须确保表格的 x 位置等于搜索栏框架的底部。您可以通过多种方式做到这一点:通过代码、Interface Builder 或两者的组合。我会让你自己找出正确的选择。

同样,这里的权衡是您必须设置两个视图控制器,并使其看起来像是同一个屏幕,即使它不是(自定义导航栏、处理转场等)。这似乎是我和我的团队愿意接受的不可避免的罪恶。从这里开始,让这一切正常工作的下一步是弄乱edgesForExtendedLayout两个视图控制器(以及contentInset表视图)上的属性。我不打算在这里编写该代码,因为根据每个人的设计,它可能会有所不同。然而,这就是它的要点。

如果您找到更简单的解决方案,请随时添加到此线程。我进行了广泛的搜索,但由于UISearchController它是一个非常新的控件,因此没有很多关于它的文档/信息(及其怪癖)。