在没有单元格的情况下为UICollectionView启用跳出/滚动 [英] Enable bounce/scroll for UICollectionView while no cells

查看:102
本文介绍了在没有单元格的情况下为UICollectionView启用跳出/滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些目的(比如拉到刷新),我需要一个UICollectionView可以在没有单元格时弹跳或滚动 - 意味着 numberOfItemsInSection: return 0

Due to some purposes(like pull to refresh), I need a UICollectionView can bounce or scroll when there is no cells -- means numberOfItemsInSection: return 0

我的代码如下:

...
        UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumInteritemSpacing = SPLIT_SPACE;
        flowLayout.minimumLineSpacing = SPLIT_SPACE;
        targetCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - TABBAR_HEIGHT)
                                                    collectionViewLayout:flowLayout];
        [targetCollection registerNib:[UINib nibWithNibName:@"DashboardCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"DashboardCell"];

        targetCollection.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        targetCollection.backgroundColor = [UIColor colorWithHex:@"#EAEAEA"];
        targetCollection.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);
        targetCollection.alwaysBounceVertical = YES; 
...
    #pragma mark - UICollectionViewDataSource
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
    {
        return 0;
    }

但是,在测试时,这个空的UICollectionView不能弹跳也不能滚动。我怀疑它与空单元格有关,但我确实需要在没有单元格时启用弹跳或滚动。
这与我的另一个问题类似: SVPullToRefresh无法启用空UICollectionView

However, when testing, this empty UICollectionView cannot bounce nor scroll. I suspect it is related to empty cell, but I do need to enable the bounce or scroll when no cell. This is similar to another of my problem:SVPullToRefresh cannot pull on an empty UICollectionView

推荐答案

 - (void)_edgeInsetsToFit {
    UIEdgeInsets edgeInsets = self.collectionView.contentInset;
    CGSize contentSize = self.collectionView.contentSize;
    CGSize size = self.collectionView.bounds.size;
    CGFloat heightOffset = (contentSize.height + edgeInsets.top) - size.height;
    if (heightOffset < 0) {
        edgeInsets.bottom = size.height - (contentSize.height + edgeInsets.top) + 1;
        self.collectionView.contentInset = edgeInsets;
    } else {
        edgeInsets.bottom = 0;
        self.collectionView.contentInset = edgeInsets;
    }
}

我在layoutSubviews

I called that method after layoutSubviews

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    _STAssetViewController *weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
        [weakSelf _edgeInsetsToFit]; 
    });
}

你可以这样试试

这篇关于在没有单元格的情况下为UICollectionView启用跳出/滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆