1

I have to show three components in one row of UICollectionView I have written the following piece of code to manipulate flowlayout

- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)collectionView.collectionViewLayout;

    CGSize size = flowLayout.itemSize;
    size.width = (Get_Bounds.width-40)/3;
    size.height = 135;   
    return size;

}

This works fine in all iOS devices but does not render good on iPhone 6 Plus it renders only two component on this device.

Avinash Sharma
  • 665
  • 1
  • 7
  • 23
  • hi [Avnash Sharma](http://stackoverflow.com/users/5945700/avinash-sharma) ,your question is duplicate check-it: http://stackoverflow.com/q/34116251/5593725 – PT Vyas Mar 22 '17 at 10:50

2 Answers2

0

you try that code

    let height = img.size.height
    let width = img.size.width
    let cellHeight = collectionView.frame.size.width/3 * height / width
    return CGSize(width: collectionView.frame.size.width/3, height: cellHeight)

get device size:

func iPhoneScreenSizes(){
let bounds = UIScreen.mainScreen().bounds
let height = bounds.size.height

switch height {
case 480.0:
    print("iPhone 3,4")
case 568.0:
    print("iPhone 5")
case 667.0:
    print("iPhone 6")
case 736.0:
    print("iPhone 6+")

default:
    print("not an iPhone")

}

}

you need to create constrain outlet and manage it to device dependent

Sagar Bhut
  • 657
  • 6
  • 28
0

In sizeForItemAtIndexPath method what you need is to give the size of the collectionViewCell Item.

so with this algorithm we divide collection view cell dimension by 3 and subtract 15(5 for each element) similar vice versa for height.

- (CGSize)collectionView:(UICollectionView *)collectionView
              layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

        return CGSize(width: (self.view.frame.size.width/3)-15, height: (self.view.frame.size.width/3)+15)

  }
Jagdeep
  • 1,158
  • 11
  • 16