Custom Margins and Restraints for Individual UICollectionViewCells - swift

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 }
}

Related

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
}

How to regulate CollectionViewCell size, by code or by storyBoard(CollectionView is inside of TableView)

I have collectionView Inside UITableViewCell, and in UICollectionViewCell I have UImageView, when I run the project there are some free space, which i don't want: how can I fix it(by code or by storyboard - it doesn't matter)
You should fix it by code. You can manage space between two UICollectionView cells by using UICollectionViewDelegateFlowLayout methods.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
}
Provide the cell height and width in the above method.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
The above method use for a vertically scrolling grid, this value represents the minimum spacing between successive rows. For a horizontally scrolling grid, this value represents the minimum spacing between successive columns.

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!

UICollectionView change cell spacing

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
}