Horizontal paging problem in UICollectionView
situation
Look at the picture directly
<UICollectionView: 0x7fc565076000;
frame = (0 0; 375 197);
clipsToBounds = YES;
gestureRecognizers = <NSArray: 0x6180000557e0>;
layer = <CALayer: 0x61000022a5a0>;
contentOffset: {187.5, 0};
contentSize: {562.5, 192.25}
>
Copy the code
The solution
There are two ways to solve the problem, there are only 11 data, 16 are needed to divide two pages, we can directly add data to 16, and then judge and process the returned cell in the dataSource. But for now too much fuss, I choose the second way ~
Modify contentSize directly
I from defines an inheritance in UICollectionViewFlowLayout Layout (LXFChatMoreCollectionLayout), let UICollectionView used when creating it
In LXFChatMoreCollectionLayout. We need to rewrite the superclass collectionViewContentSize swift, amend the contentSize out for our own create newSize can the code below
override var collectionViewContentSize: CGSize {
let size: CGSize = super.collectionViewContentSize
let collectionViewWidth: CGFloat = self.collectionView! .frame.size.widthlet nbOfScreen: Int = Int(ceil(size.width / collectionViewWidth))
let newSize: CGSize = CGSize(width: collectionViewWidth * CGFloat(nbOfScreen), height: size.height)
return newSize
}
Copy the code
Note: The function of ceil is to find the smallest integer not less than a given real number. Ceil (2) = ceil (1.2) = cei (1.5) = 2.00
The effect
For how to layout items horizontally, please refer to ios-Swift UICollectionView Horizontal page Scrolling, Cell Left and right Layout.
Attached is related project: Swift 3.0 high imitation wechat