这是我的UICollectionViewFlowLayout子类:
@implementation MyCollectionViewFlowLayout
- (id)init
{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (void)setup
{
self.itemSize = CGSizeMake(320, 320);
self.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
self.minimumInteritemSpacing = 0;
self.minimumLineSpacing = 0;
self.scrollDirection = UICollectionViewScrollDirectionVertical;
}
- (void)prepareLayout {
[super prepareLayout];
}
- (CGSize)collectionViewContentSize {
return self.itemSize;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray* allAttributesInRect = [super layoutAttributesForElementsInRect:rect];
return allAttributesInRect;
}
- (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes* attributes = [super layoutAttributesForItemAtIndexPath:indexPath];
return attributes;
}
@end
当我执行以下操作时,收藏夹视图将不会滚动:
MyCollectionViewFlowLayout* flowLayout = [[MyCollectionViewFlowLayout alloc] init];
但是,如果我这样做:
UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(320, 320);
flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 0;
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
然后,收藏夹视图滚动良好。我在UICollectionViewFlowLayout子类中做错了什么?
问题是collectionViewContentSize
- (CGSize)collectionViewContentSize {
return self.itemSize;
}
在这里返回itemSize意味着您的集合视图的整个contentSize仅为一项的大小。尝试删除该代码,或将其更改为
- (CGSize)collectionViewContentSize {
[super collectionViewContentSize];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句