I have a Collection View can show about 5 cells at a time, and I want it be paging enabled. But the page stretch out the view's bounds. I want it to show 10 cells a page, and scroll the view to snap to other page. You can see below pic:
My current code is like this:
- (void)setUpCollectionView
{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
self.collectionView.pagingEnabled = YES;
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:self.collectionView];
self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
self.collectionView.backgroundColor = [UIColor whiteColor];
UINib *nib = [UINib nibWithNibName:@"CollectionViewCell" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:@"Cell"];
}
- (void)updateViewConstraints {
[super updateViewConstraints];
NSLayoutConstraint *collectionViewLeftSideConstraint = [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
NSLayoutConstraint *collectionViewTopConstraint = [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
NSLayoutConstraint *collectionViewBottomConstraint = [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.nextKeyboardButton attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
NSLayoutConstraint *collectionViewRightSideConstraint = [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
[self.view addConstraints:@[collectionViewBottomConstraint, collectionViewLeftSideConstraint, collectionViewRightSideConstraint, collectionViewTopConstraint]];
}