在NavigationBar后面的UICollectionView头中隐藏UISearchBar

时间:2014-04-02 12:55:09

标签: ios iphone uicollectionview uisearchbar

我已将UISearchBar添加到header的{​​{1}}。我想仅在用户想要查看时显示CollectionView。我想将它隐藏在UISearchBar后面,就像 Whats App 那样。

我使用NavigationBar添加UICollectionReusableView

1 个答案:

答案 0 :(得分:3)

UICollectionViewUIScrollView的子类,因此您需要为集合视图设置委托,并实现如下的滚动视图委托方法。

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    CGFloat contentOffset = scrollView.contentOffset.y;

    // If the header is already shown, it will be hidden right when you start scrolling down
    if (contentOffset >= 0) {
        if (self.showHeader) {
                [UIView animateWithDuration:.5 animations:^{
                    self.collectionView.frame = CGRectMake(0, -HEADER_HEIGHT, 320, COLLECTION_VIEW_HEIGHT+HEADER_HEIGHT);
                } completion:^(BOOL finished) {
                }];
                self.showHeader = NO;
            }
    }
}

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    CGFloat contentOffset = scrollView.contentOffset.y;

    // When you scroll up to the point that the header can be seen, reveal it
    // Adjust the offset limit to suit your need
    if (contentOffset < -HEADER_HEIGHT+10) {
        if (!self.showHeader) {
                self.collectionView.frame = CGRectMake(0, 0, 320, COLLECTION_VIEW_HEIGHT);
                self.showHeader = YES;
            }
    }
}