Sampath Kumar
Sampath Kumar

Reputation: 41

How to make a UICollectionViewCell span entire row?

Context

Problem

Question

How do I ensure that the width of the collection view cell matches that of the container width?

Upvotes: 1

Views: 928

Answers (2)

Saqib Omer
Saqib Omer

Reputation: 5477

It is because you are not using autolayout. You can achieve this entirely in storyboard or through code. Using Storyboard

  1. Add UICollectionView in your UIViewController in storyboard.
  2. Drag UICollectionView to fill up in your UIViewController.
  3. Add Constraints as shown in image. enter image description here
  4. Drag UICollectionView to fill up in UICollectionView.
  5. Add UILabel (Or whatever you want to have in cell). Also add constraints in that element.
  6. Build and run.

Upvotes: 0

Yedidya Reiss
Yedidya Reiss

Reputation: 5326

1) Implement the function of cell size and return the collection width:

-(CGSize) collectionView: (UICollectionView*) collectionView layout:(UICollectionViewLayout*) collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*) indexPath {
    CGFloat height = 50; //set the wanted height
    return CGSizeMake(collectionView.frame.size.width,height);
}

2) Reload the collection when the screen size change (i.e. orientation change).

Upvotes: 1

Related Questions