UICollectionView change cell spacing - swift

I am working on a horizontal collection view which have cells with dynamic size depends on the label size. I want to set cell in the center of the collection view. I am able to achieve this using
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets { return UIEdgeInsetsMake(0, leftinset, 0, leftinset) }
Now I want to maintain cell spacing. I had Tried
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return 0.0
}
But it is not working in this collection view it also reflect on my other collection view. Can any one help me
This is my current output
Image of output

Use this methods..
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat
{
return 0.0
}

Related

Custom Margins and Restraints for Individual UICollectionViewCells

I have a 2-item UICollectionView with Two Cells. My goal is that each time you swipe, at any given time, you will see only an individual cell that is spaced from the margins from left and right equally. If the cells are also different sizes, that must mean that each cell must have individually unique constraints from the margin.
Questions:
How do I go about adding constraints to these cells so that they remain evenly spaced across multiple iPhone layouts?
How do I make sure that a cell does not "bleed" to the next, making sure that ay ant given time, a single cell is seen on the screen?
Thank You!
You've to implement three methods in UICollectionViewDelegateFlowLayout to make it work right.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
Return calculated values to each of these and the cells will layout correctly. Here is a sample implementation.
extension ViewController: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let spacing: CGFloat = 16 * 2
return .init(width: collectionView.frame.size.width - spacing, height: 100)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 16 }
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 0 }
}

UICollectionViewCell: How to Center Cells For UICollectonView

I have a 2-item UICollectionView and I want them to move in a way that whenever the user swipes to the next cell the cell is centered and at any given point, only one cell is seen.
How do I go about doing this programmatically? Also, how do I do this so that it remains the same if I were to switch the simulator to a different iPhone?
Set your view controller as collection view's delegate. Implement UICollectionViewDelegateFlowLayout in your view controller and provide sizeForItem as below.
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.size.width, height: cellHeight))
}
Set section insets and line spacing:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return .zero
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}

UICollectionView different size cell and item count

Using UICollectionView flow delegate, I'm trying to accomplish this but I still need to remove the huge spacing in the two cell areas.
My Code :
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.row == 0 || indexPath.row == 5 {
return CGSize(width: (getWidth()/2) , height: (getWidth()/2))
}
return CGSize(width: (getWidth()/4) , height: (getWidth()/4))
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
Expected :
It is impossible to do this using plain UICollectionViewLayout. By its nature, UICollectionViewLayout is a "line-braking-layout" and line-height is the height of its tallest element. Please see the following:
Apple docs on using UICollectionViewFlowLayout
However, the behavior you want can be achieved by subclassing UICollectionViewFlowLayout. Here is a guide on how to do it:
Creating custom layouts

UICollectionViewCell spacing to be zero across all device sizes

I'm having trouble setting my UICollectionViewCells to be all the same width and height so that there are no lines/spacing between each cell. Testing on an smaller iPhone like 5S and SE shows no lines between the cells, but larger iPhones such as 6,7 Pluses do - image description below. My view controller adopts UICollectionViewDelegateFlowLayout, and it has a collectionView.
In viewDidLoad, I set the collectionView's delegate to be the view controller.
override func viewDidLoad() {
super.viewDidLoad()
self.viewController.delegate = self
}
I use the following methods to set the cell size and spacing.
let screenWidth:CGFLoat = UIScreen.main.bounds.width
let cellsPerRow:CGFLoat = 4
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let side:Double = Double(self.screenWidth/self.cellsPerRow)
return CGSize(width: side, height: side)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
iPhone 5S, No Lines
iPhone 7 Plus, Lines
Any recommendations or advice to get rid of these line/spacing is greatly appreciated!

Need UICollectionViewCell padding using UICollectionViewLayout

I am working with UICollectionView which has custom UICollectionViewLayout.I need spacing around UICollectionViewCell like i can get if i use UICollectionViewFlowLayout with this minimumInteritemSpacingForSectionAtIndex and minimumLineSpacingForSectionAtIndex.
This properties are not in UICollectionViewLayout. So how i can manage spacing between UICollectionViewCell in swift 2.1 or any idea?
I have tried this
let bcolor : UIColor = UIColor.whiteColor()
dataCell.layer.borderColor = bcolor.CGColor
dataCell.layer.borderWidth = 0.9
dataCell.layer.cornerRadius = 3
any better idea?
After a stupid amount of research I think that this code provides a skeleton for what you need:
import UIKit
class UICVC: UICollectionViewController, UICollectionViewDelegateFlowLayout {
// You probably want to implement this
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
return CGFloat(10)
}
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
return CGFloat(10)
}
}
let uicv = UICollectionView()
uicv.delegate = UICVC()
This code ensures that the Collection View's cells are the right size. Assuming your collection view is a standard grid, this should work well for sizing the cells. Positioning of content within cells is another matter, of course.