我已将 UISearchBar
添加到 CollectionView
的 header
中。我只想仅在用户想要查看时才显示 UISearchBar
。我想将其隐藏在 NavigationBar
后面,就像 Whats App 那样。
我使用了 UICollectionReusableView
来添加 SearchBar
。
最佳答案
UICollectionView
是 UIScrollView
的子类,因此您需要为 Collection View 设置委托(delegate)并实现如下所示的 ScrollView 委托(delegate)方法。
- (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;
}
}
}
关于ios - 将 UICollectionView header 中的 UISearchBar 隐藏在 NavigationBar 后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22812199/