如何在Notes.app中复制UISearchBar滚动行为?

Ind*_*oor 7 iphone scroll uitableview uisearchbar

当您向下滚动tableView(Notes.app)时,搜索栏会保持固定为顶部,但是当您向上滚动tableView时,搜索栏将被隐藏.这就是我想要的.

看滚动指示器,看起来UISearchBar是表滚动视图的子视图.但是如果我在Interface Builder中向tableView添加一个搜索栏,那么它总是像常规行一样滚动附加到tableView.

joe*_*ick 4

在表视图中,将一个空白的透明 UIView ( searchBarPlaceholderView) 放入 tableHeaderView 槽中,其尺寸与 UISearchBar 完全相同。然后将 UISearchBar ( searchBar) 添加到顶级视图。

然后,在表视图委托中添加

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect sbFrame = searchBar.frame;
    sbFrame.origin.y = searchBarPlaceholderView.frame.origin.y - scrollView.contentOffset.y;

    // it cannot move from the top of the screen
    if (sbFrame.origin.y > 0) {
        sbFrame.origin.y = 0;
    }

    searchBar.frame = sbFrame;
}
Run Code Online (Sandbox Code Playgroud)