Uicollectionview Cell Spacing Based on Device Screen Size

How to limit collection view spacing based on screen

You can try below codes :

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0) // up to you
layout.itemSize = CGSize(width: screenWidth/3, height: screenWidth/3)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0

yourCollectionView.collectionViewLayout = flowLayout

UICollectionView Cell Spacing based on device screen size

Step 1: Implement UICollectionViewDelegateFlowLayout(if not done).

Step 2: Use delegate method of UICollectionViewDelegateFlowLayout.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSizeMake((UIScreen.mainScreen().bounds.width-15)/4,120); //use height whatever you wants.
}

Step 3: Go to XIB or StoryBoard where you have your CollectionView.

Step 4: In XIB or StoryBoard where you have your CollectionView click on CollectionView.

Step 5: Go to InterfaceBuilder, then in second last tab (ie: Size Inspector) set Min Spacing

For Cells = 5

For Lines = 5

That it.

Note:

  • This solution is for if you wants to make spacing between cell = 5.
  • If your spacing is different then 5, then you need to change value 15 in delegate method.
  • Ex: Spacing between cell = 10, then change delegate 15 to 10x3 = 30

return CGSizeMake((UIScreen.mainScreen().bounds.width-30)/4,120);

  • And set Min Spacing

For Cells = 10

For Lines = 10

and vice versa.

Swift 3 Version

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = UIScreen.main.bounds.width
return CGSize(width: (width - 10)/3, height: (width - 10)/3) // width & height are the same to make a square cell
}

How to resize the collection view cells according to device screen size?

Implement sizeForItemAt method according to the view's frame size

Swift

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

let height = view.frame.size.height
let width = view.frame.size.width
// in case you you want the cell to be 40% of your controllers view
return CGSize(width: width * 0.4, height: height * 0.4)
}

Objective C

- (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);
}

change UICollectionView Cell size depending on the screen size

its easy to detect screen size, for each & every screen size of iPhone/iPad. Just follow this stuff.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{

if UIDevice().userInterfaceIdiom == .phone
{
switch UIScreen.main.nativeBounds.height
{
case 480:
print("iPhone Classic")
case 960:
print("iPhone 4 or 4S")

case 1136:
print("iPhone 5 or 5S or 5C")

case 1334:
print("iPhone 6 or 6S")
case 2208:
print("iPhone 6+ or 6S+")
default:
print("unknown")
}
}

if UIDevice().userInterfaceIdiom == .pad
{
if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad &&
(UIScreen.main.bounds.size.height == 1366 || UIScreen.main.bounds.size.width == 1366))
{
print("iPad Pro : 12.9 inch")
}
else if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad &&
(UIScreen.main.bounds.size.height == 1024 || UIScreen.main.bounds.size.width == 1024))
{
print("iPad 2")
print("iPad Pro : 9.7 inch")
print("iPad Air/iPad Air 2")
print("iPad Retina")
}
else
{
print("iPad 3")
}
}

}

So use this to set height & width as per your requirements.
Even you can perform/set layout for particular screen.

Hope it helps you.

Dynamically sizing width of UICollectionView cell width for various iPhone screen sizes

You need to take into account the collectionViews edge insets and minimumInteritemSpacing before creating a size. You should really implement the UICollectionViewFlowLayoutDelegate methods for laying out the collectionView. Your width should be (screenSize - left inset - right inset - minimumInteritemSpacing) / 2.

EDIT

Why not check what device it is and then return a size based on that?

e.g

if iPhone6 {
return size / 2
} else {
return size
}

To check what device it currently is refer to the following answer.

UICollectionViewCell Size According to Screen/ FrameSize Swift

Here is your swift code:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

let numberOfCell: CGFloat = 3 //you need to give a type as CGFloat
let cellWidth = UIScreen.mainScreen().bounds.size.width / numberOfCell
return CGSizeMake(cellWidth, cellWidth)
}

Here the type of numberOfCell must be a CGFloat because UIScreen.mainScreen().bounds.size.width return a CGFloat value so if you want to divide it with numberOfCell then type numberOfCell must be CGFloat because you can not divide CGFloat with Int.



Related Topics



Leave a reply



Submit