0

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: enter image description here

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]];
}
yong ho
  • 3,576
  • 9
  • 34
  • 67
  • What are the other squares at the bottom (Coffee &... etc.)? I'm not sure what you have working so far, but I think I understand what you are trying to do. – CaptJak Oct 29 '14 at 01:56
  • @CaptJak Never mind the square. That's not the problem. See the right, it's stretch to the right. it suppose to stop at the Shopping square. How to do that? – yong ho Oct 29 '14 at 02:07
  • 1
    I get it. You can probably find your answer [here](http://stackoverflow.com/questions/13492037/targetcontentoffsetforproposedcontentoffsetwithscrollingvelocity-without-subcla), although you would never guess it from the title of the question. It seems like you are having the same problem. – CaptJak Oct 29 '14 at 02:22
  • Yes, targetContentOffset is your solution. – i_am_jorf Oct 29 '14 at 03:15
  • @jeffamaphone Not working. Is that because I layout my collection view using auto layout?. It will work with initWithFrame, right? But, I am adding a collection view in a keyboard extension. the keyboard's frame or bounds actually are all zeros. So I must use auto layout. – yong ho Oct 29 '14 at 03:17
  • I don't know. I layout mine using auto layout. Autolayout will override an explicitly set frame. – i_am_jorf Oct 29 '14 at 03:20
  • @yongho , Have you found any solution for this problem? – Mohammad Zaid Pathan Sep 18 '15 at 05:19

0 Answers0