0

I have implemented collectionView in Swift,

but the spacing between the cell increases when i check it in bigger screen iPhones.

This is the example of iphoen 5s 1:

This is the example of iphone 6 2:

here is my xcode design 3:

Where i am making mistake? can any one help me here..

Thank you in advance !

Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47
Krunal
  • 6,440
  • 21
  • 91
  • 155

4 Answers4

1

This solved my problem..

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
    {
        return CGSizeMake((UIScreen.mainScreen().bounds.width-15)/2,208); //use height whatever you wants.
    }
Krunal
  • 6,440
  • 21
  • 91
  • 155
0

i think you should use auto layout to make your cell's width half of CollectionView's width and minus some point, but i am not sure if this can be set in storyboard,i mostly do this in code ,use 'collectionViewFlowLayout.setItemSize' to set the item size relative to the screen width,or UICollectionViewDelegateFlowLayout will do as well;

Han Solo
  • 1
  • 3
0

Do like this :

 func collectionView(collectionView: UICollectionView,
        layout collectionViewLayout: UICollectionViewLayout,
        sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
    {
           collectionData.contentInset=UIEdgeInsetsMake(0,0,0,0)
        let screensize:CGRect=UIScreen.mainScreen().bounds
        let width=screensize.width

        if (width==320)
        {
            let size:CGSize=CGSizeMake(129,140)
            collectionData.contentInset=UIEdgeInsetsMake(0,10,0,10)
            return size
        }
        else if(width==375)
        {
            let size:CGSize=CGSizeMake(166,181)
            collectionData.contentInset=UIEdgeInsetsMake(0,0,0,0)
            return size
        }
        else if(width==414)
        {
            if(UIDevice.currentDevice().orientation .isLandscape)
            {
                let size:CGSize=CGSizeMake(300,185)
                collectionData.contentInset=UIEdgeInsetsMake(0,0,0,0)
                print("com,ing inside orientatiom");
                return size

            }
            let size:CGSize=CGSizeMake(177,185)
            collectionData.contentInset=UIEdgeInsetsMake(0,0,0,0)
            return size
        }
        let size:CGSize=CGSizeMake(358,292)
        return size


    }
Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47
0

You can be adjusted common space for all device - objective c version

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

   CGFloat height = self.view.frame.size.height;
   CGFloat width  = self.view.frame.size.width;
   // in case you you want the cell to be 40% of your controllers view
   return CGSizeMake(width*0.4,height*0.4)
 }
codercat
  • 22,873
  • 9
  • 61
  • 85