UICollectionView items spacing is not constant - ios

I'm trying to show a collection view with each cell displaying a string inside border. I have set the direction to VERTICAL. So there can be multiple cells in each row with dynamic size. But cells are being displayed with different inter items space.
Current State of Collection View:
My collection view configuration is below:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if(collectionView == tagsCollectionView)
{
if(self.propertyTypes.count > 0)
{
if(indexPath.row == 0)
{
return CGSize(width: "You searched for properties in: ".width(withConstrainedHeight: 25, font: UIFont(name: "Avenir-Book", size: 13)!), height: 30)
}
return CGSize(width: self.propertyTypes[indexPath.row - 1].width(withConstrainedHeight: 25, font: UIFont(name: "Avenir-Book", size: 13)!), height: 30)
}
return CGSize(width: 0, height: 0)
}
return "String".size(withAttributes: nil)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
However, the collection view works absolutely fine when the direction is HORIZONTAL.

You can use a pod: https://github.com/mischa-hildebrand/AlignedCollectionViewFlowLayout to achieve your functionality.
You can achieve left, right, justified etc. with this library.
Use it like:
let alignedFlowLayout = collectionView?.collectionViewLayout as? AlignedCollectionViewFlowLayout
alignedFlowLayout?.horizontalAlignment = .left
alignedFlowLayout?.verticalAlignment = .top
Check library for more detailed info.

Related

Dynamic item height with equal spacing

What I want to achieve is a layout like this.
Inside to box there will be other content like image, text, button and so on. what I want is make the box sizing dynamic so that it's it takes the height of it's content. The first option came to my mind is use a collection view flow layout.
After some trial and error I end up with this layout.
My Problem is the inter item spacing I want them to be equal, is there any way to achieve this?
Any suggestion I greatly appreciated.
What I have tried so far.
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return data.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCollectionViewCell", for: indexPath) as! MyCollectionViewCell
cell.configure(title: data[indexPath.row].title)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let spacing: CGFloat = 10
let numberOfItemsPerRow:CGFloat = 2
let spacingBetweenCells:CGFloat = 10
let totalSpacing = (2 * spacing) + ((numberOfItemsPerRow - 1) * spacingBetweenCells)
let approximateWidthOfContent = view.frame.width - 32
let size = CGSize(width: approximateWidthOfContent, height: 1000)
let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15)]
let estimatedFrame = NSString(string: data[indexPath.row].title).boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
if let collection = self.collectionView{
let width = (collection.bounds.width - totalSpacing)/numberOfItemsPerRow
return CGSize(width: width, height: estimatedFrame.height + 50)
}else{
return CGSize(width: 0, height: 0)
}
}
}
I know there are similar questions but I could not find any which meets my requirement.
After following a tutorial from raywenderlich I finally managed to achieve the layout I wanted, Thanks people who have helped me suggesting with important clues.
What I achieved
The link for the Instructions I have followed is here.

collection view cell spacing not working on all devices

I have a collection view where two cells are in a row. Both have the same size and I want to have the same distance between them no matter how many cells are in the collection view.
Now I have found out that the distance only does not work with the iPhone 11 Pro. Be it on a real device or in a simulator.
Here are two images facing this problem:
This is my code for the size and spacing:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if storeModel.count != 0 {
let bounds = collectionView.bounds
let heightVal = self.view.frame.height
let widthVal = self.view.frame.width
let cellsize = (heightVal < widthVal) ? bounds.height/2 : bounds.width/2
return CGSize(width: cellsize - 10 , height: cellsize - 10 )
} else {
let width = collectionView.frame.width
let height = collectionView.frame.height
return CGSize(width: width - 20, height: height)
}
}
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 10
}

Swift: Change the width of cell Layout in UICollectionView when add items

I'm new to swift , i want to make a UICollectionViewCell layout to be dynamic example: if we have an item added to look like this:
1.if we have one item it look like this
2.If we have two items added to look like this:
3. if we have item 3 again to look like this :
This process proceeds this way, as long as there are items
Thanks!
This is method of UICollectionViewDelegateFlowLayout for the size of items.
Just check for total item count odd/even and based on that decide you cell width.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if arrList.count % 2 == 0 {
let width = (collectionView.frame.width-20)/2
let height : CGFloat = 100.0
return CGSize(width: width, height: height)
}else {
if arrList.count-1 == indexPath.row {
let width = collectionView.frame.width-10
let height : CGFloat = 100.0
return CGSize(width: width, height: height)
}else{
let width = (collectionView.frame.width-20)/2
let height : CGFloat = 100.0
return CGSize(width: width, height: height)
}
}
}
This is interspacing methods.
//these methods are to configure the spacing between items
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0,5,5,5)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10
}

collection view cell Hight and width in swift

collection view had multiple images working fine. it's showing like this
But i want show like this way
this is code of collection view cell
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 20, right: 0)
let width = UIScreen.main.bounds.width
layout.itemSize = CGSize(width: width/2 , height: width/2 )
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView!.collectionViewLayout = layout
Just implement some UICollectionViewDelegateFlowLayout methods to get the correct size and spacing of the UICollectionViewCells.
Example:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let cellSize = CGSize(width: (collectionView.bounds.width - (3 * 10))/2, height: 120)
return cellSize
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
{
return 10
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
let sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
return sectionInset
}
Just change the size and spacing values according to your requirement.
Add this method on your viewcontroller
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
return CGSize(width: ((self.view.frame.size.width/2) - 10), height: 255)
}
Try,
collectionViewCellImageView.contentMode = .scaleToFill
You can try to set minimum spacing for cell & line for collection view and implement the bellow delegate
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (collectionView.frame.width/2) - 5, height: collectionView.frame.width/2)
}

Swift 3 - UICollectionView Center Cell with Padding

I have a UICollectionView that scrolls horizontally and I would like to have one cell visible on the screen at a time.
I am programmatically setting the cell width and height in the following method:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath as IndexPath) as! Cell
let width = collectionView.bounds.size.width - 40
let height = collectionView.bounds.size.height - 40
cell.bounds.size = CGSize(width: CGFloat(width), height: CGFloat(height))
return cell
}
And after struggling to figure out how to work with insets, spacing, section insets (both programmatically and in the inspectors in XCode) I can't seem to figure out what I need to set and where I need to set it.
Here is a diagram of my desired spacing:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
return CGSize(width: UIScreen.main.bounds.width - 40, height: yourCollectionViewHeight - 40)
}
left 20 - add Footer Size and Header Size in storyboard with width = 20
spacing between cells - add Minimum Spacing in storyboard = 40
I could have made some mistakes and you will not get exactly your layout, but I hope you would get the idea.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width * 0.7, height: 300)
}
func collectionView(_ collectionView: UICollectionView, layoucollectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let margin = collectionView.frame.width * 0.3
return UIEdgeInsets(top: 10, left: margin / 2, bottom: 10, right: margin / 2)
}
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 collectionView.frame.width * 0.3 / 2
}
If you want single cell visible on the screen at a time then important thing is that you should enable paging for your CollectionView. You can do it in swift as below :
collectionView.isPagingEnabled = true
Now conform your ViewController class to UICollectionViewDelegateFlowLayout and implement it's method sizeForItemAt indexPath which returns CGSize for your collectionview cell. For Example:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 200, height: 200)
}

Resources