由于某些目的(比如拉到刷新),我需要一个UICollectionView可以在没有单元格时弹跳或滚动 - 意味着numberOfItemsInSection:
返回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 cannot pull on an empty UICollectionView
答案 0 :(得分:48)
答案 1 :(得分:14)
答案 2 :(得分:0)
您可以尝试以下方法:
collectionView.isScrollEnabled = false
答案 3 :(得分:0)
快速解决方案
collectionView.alwaysBounceVertical = true
答案 4 :(得分:-3)
- (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
之后调用了该方法- (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];
});
}
你可以这样试试