ios - 将 UICollectionView header 中的 UISearchBar 隐藏在 NavigationBar 后面

标签 ios iphone uicollectionview uisearchbar

我已将 UISearchBar 添加到 CollectionViewheader 中。我只想仅在用户想要查看时才显示 UISearchBar。我想将其隐藏在 NavigationBar 后面,就像 Whats App 那样。

我使用了 UICollectionReusableView 来添加 SearchBar

最佳答案

UICollectionViewUIScrollView 的子类,因此您需要为 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/

相关文章:

UIAlertController/UIAlertView 的 iOS/Swift 测试

iOS 如何知道我正在穿越特定位置

ios - UICollectionView 和弹性单元格

swift - 切换到另一个聊天时无法从聊天 View Controller 中删除消息

ios - 滚动时奇怪的消失项目

ios - 如何让 uitableview 一次加载多个部分?

ios - UIGraphicsGetImageFromCurrentImageContext() 返回 nil

ios - UIColor 返回暗模式颜色的错误值

ios - Today Widget 在 iOS 8 设备上没有内容

ios - 在 iOS 设备上交互显示 ppt