0

I have a collection view in my view controller.

enter image description here

but it works fine in iPhone 6-7 and above it. but that show just one cell in iPhone 4 and 5!

iPhone 5: enter image description here

iPhone 6: enter image description here

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254

2 Answers2

1

Set your CollectionViewCell size dynamic for each screen. You can try something like this...

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(_myCollection.frame.size.width/2-13, _myCollection.frame.size.width/2-13);
}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Vineet Rai
  • 80
  • 7
1

It works fine on 4.7 inches and above iPhones screen sizes because their is width size is enough to display tow cells (with their default widths).

What you should do is to determine what's the size of the cell, is to implement:

collection​View(_:​layout:​size​For​Item​At:​) method from UICollection​View​Delegate​Flow​Layout protocol. as follows:

// don't forget to conform to UICollectionViewDelegateFlowLayout protocol:
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout {
    //...

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let screenWidth = UIScreen.main.bounds.width

        // if you want to let the cell to be a square,
        // you should let the height equals to the width
        // or you can -of course- set the deired height
        return CGSize(width: screenWidth / 2, height: screenWidth / 2)
    }

    // ...
}

By implementing this, the cells width will be the half of the screen, no matter what the size of the device screen.

Remark: make sure that the minimum spaces for the collection view are zeros:

enter image description here


Also, checking this Q&A might be useful to your case.

Hope this helped.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143