Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Horizontal CollectionView not scrolling all the way

I have a single row collectionview that scrolls horizontally. The collectionview works but it cuts off the second to last cell (labeled orange) and hides the last cell.

I have 5 cells, each of size 166x166. I have checked the contentsize of the viewlayout and it's width is 830 which is correct, yet I am unable to scroll that far.

Here is the settings in Storyboard:

enter image description here

And here is the code for the collectionview.

#pragma mark Collection View Methods

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [array count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FeaturedCell" forIndexPath:indexPath];

    UILabel *label = (UILabel *) [cell viewWithTag: 100];
    label.text = [array objectAtIndex:indexPath.row];

    [cell.layer setBorderWidth:2.0f];
    [cell.layer setBorderColor:[UIColor whiteColor].CGColor];

    return cell;
}
like image 660
James Fazio Avatar asked Oct 31 '22 10:10

James Fazio


2 Answers

The new IB default for view sizes is 600 points. If you don't use auto layout or another means to constrain the sizes, the frame will extend off screen and won't be visible. If this is happening at 320 px wide, you'll lose 240 points, which if your cells are 166 points wide, means you lose about 1 1/2 cells. About what's missing.

Make sure the collection view has a trailing constraint with constant 0 to the superview edge (or other means to ensure the sizes match, such as "equal" widths to superview).

like image 108
Mike Sand Avatar answered Nov 15 '22 07:11

Mike Sand


Setting minimumLineSpacing and minimumInteritemSpacing to an equal value solved my problem for horizontal collection view.

like image 25
roy h Avatar answered Nov 15 '22 06:11

roy h