UICollectionView align logic missing in horizontal paging scrollview

前端 未结 16 1183
天命终不由人
天命终不由人 2020-12-07 06:49

I\'ve got a UICollectionView, which works ok, until I start scrolling. Here some pics first: \"enter

16条回答
  •  旧巷少年郎
    2020-12-07 07:29

    This is the same problem that I was experiencing and i posted my solution on another post, so I'll post it again here.

    I found a solution to it, and it involved subclassing the UICollectionViewFlowLayout.

    My CollectionViewCell size is 302 X 457 and i set my minimum line spacing to be 18 (9pix for each cell)

    When you extend from that class there are a few methods that need to be over-ridden. One of them is

    • (CGSize)collectionViewContentSize

    In this method, I needed to add up the total width of what was in the UICollectionView. That includes the ([datasource count] * widthOfCollectionViewCell) + ([datasource count] * 18)

    Here is my custom UICollectionViewFlowLayout methods....

    -(id)init
    {
        if((self = [super init])){
    
           self.itemSize = CGSizeMake(302, 457);
           self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
           self.minimumInteritemSpacing = 0.0f;
           self.minimumLineSpacing = 18.0f;
           [self setScrollDirection:UICollectionViewScrollDirectionHorizontal];
       }
        return self;
    }
    
    
    
    -(CGSize)collectionViewContentSize{
       return CGSizeMake((numCellsCount * 302)+(numCellsCount * 18), 457);
    }
    

    This worked for me, so I hope someone else finds it useful!

提交回复
热议问题