I have a collection view in my view controller.
but it works fine in iPhone 6-7 and above it. but that show just one cell in iPhone 4 and 5!
I have a collection view in my view controller.
but it works fine in iPhone 6-7 and above it. but that show just one cell in iPhone 4 and 5!
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);
}
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:
collectionView(_:layout:sizeForItemAt:) method from UICollectionViewDelegateFlowLayout 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:
Also, checking this Q&A might be useful to your case.
Hope this helped.