我已将UISearchBar
添加到header
的{{1}}。我想仅在用户想要查看时显示CollectionView
。我想将它隐藏在UISearchBar
后面,就像 Whats App 那样。
我使用NavigationBar
添加UICollectionReusableView
。
答案 0 :(得分:3)
UICollectionView
是UIScrollView
的子类,因此您需要为集合视图设置委托,并实现如下的滚动视图委托方法。
- (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;
}
}
}