Say you have a UICollectionView but you want it to look like a UITableView with rows that span the entire row. Here’s how you you do it.

Implement the UICollectionViewDelegateFlowLayout

Screen Shot 2017-01-09 at 2.33.50 PM.png

And then add this method

Screen Shot 2017-01-09 at 2.38.01 PM.png

If you do this, you too can have the following beautiful layout

Screen Shot 2017-01-09 at 2.38.57 PM.png

Code for easy copy

@interface ViewController : UIViewController<UICollectionViewDelegateFlowLayout>
@end
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(collectionView.frame.size.width, 26);
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        let size = CGSize(width: collectionView.frame.width, height: 26)
        return size
}
Advertisements