粘滞的search栏和节标题行为类似于本机通讯录应用程序

我想在iPhone联系人应用程序中复制search栏的粘性行为。

正常阶段

当用户向下滚动视图时, search栏也随着视图一起下移

用户向下滚动视图

如果用户向上滚动,则表格相应地滚动,具有以下两种行为:

(1)search栏保持固定在顶部 ,并且
(2) 后面的部分标题在search栏下方正确停止

用户向上滚动

当下一节标题出现时,前面的标题在search栏下消失:

用户向上滚动

注意:段落索引控件(右侧的az)也出现在search栏的顶部 。 Ergo,摆弄contentInset会把它的索引控件一起向下推。

我创build了一个自定义UIViewController ,添加了一个UITableView ,将其contentInset设置为search栏的高度。 我创build了一个UIView ,添加了search栏作为它的子视图,然后将UIView添加到UITableView 。 但是,如上所述,当用户滚动时,节标题仍然保持在零位,而不是在标题高度。 此外,节标题索引控制​​位置受到不利影响。

我会很感激解决这个问题。

把所有事情都做好是很有必要的,但是我必须certificate,至less几乎可以重新创造这种行为。
看看我创build的这个GitHub项目: https : //github.com/fabiankr/TableViewSearchBar

事实上,这并不复杂:
1)将search栏直接添加到表视图,并设置tableView的contentInset
2)在-scrollViewDidScroll:调整search栏的框架

但是有一些注意事项需要注意:
1)当将表格视图滚动到顶部时,部分标题短暂地出现在search栏上方。 为了解决这个问题,当滚动到顶部时,设置search栏的位置
2)你的内容控制器需要是UIViewController一个子类而不是UITableViewController ,因为UISearchDisplayController将调光视图添加到控制器的视图。 由于表格视图控制器的view是一个表格视图,所以在滚动表格视图时,调光视图将会出现错误的位置。

唯一不能使用公共API的事情是让表右侧的部分索引控制与search栏重叠。 这只是一个小的事情,即使没有它的行为是非常相似的联系人应用程序中find。

为了达到完全相同的行为,你将不得不使用私人API。 在UITableView上有一个名为_setPinsTableHeaderView:的方法_setPinsTableHeaderView:需要使用。 示例项目包含以下两个实现:1)仅公共API和2)私有API以获取段索引控件与search栏重叠。
提醒 :当您计划将应用程序提交到App Store时,您不应该使用私有API!

我实现这种行为的方式是将我的浮动单元格从UITableView中分离出来,并使其成为UITableView的子视图,并为scrollViewDidScroll中的浮动单元格设置animation。 就这样,UITableView向下滚动到足以揭示浮动单元我也卡住tableview中的一个不可见的单元格,当scrollViewDidScroll被调用时,被浮动单元覆盖。

 - (void)viewDidLoad { // add floating cell to tableview FloatingCell *cell = [[FloatingCell alloc] init]; [self.tableView addSubview:cell]; self.floatingCell = cell; } // overwrite scrollViewDidScroll - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGRect floatingCellFrame = floatingCell.frame; CGFloat floatingCellHeight = floatingCellFrame.size.height; // when contentOffset is is more then cellHeight scroll floating cell if (scrollView.contentOffset.y > floatingCellHeight) { floatingCellFrame.origin.y = -scrollView.contentOffset.y + floatingCellHeight; // when contentOffset is less then cellHeight stick it to the top of UITableView else if (scrollView.contentOffset.y < floatingCellHeight) floatingCellFrame.origin.y = 0; floatingCell.frame = floatingCellFrame; } 

在滚动时你可能需要添加一些情况,所以浮动单元看起来不会跳跃,但是这应该开始。 祝你好运!