我有一个Collection View可以一次显示大约5个单元格,我希望它可以启用分页。但是页面延伸了视图的界限。我希望它在页面上显示10个单元格,并滚动视图以捕捉到其他页面。您可以在下面看到图片:
我目前的代码是这样的:
- (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]];
}