Custom collection view cell not working properly - ios

Trying to create a custom cell but when I am adding label in the cell collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) doesn't work.
Without label cell UI:
and the iPad simulator UI is:
But when I am adding label into the cell:
The iPad Simulator UI becomes :
In UICollectionViewDelegateFlowLayout in the method:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let emptySpace = 87*2 + 10
let remainingWidth = Int(self.view.frame.width) - emptySpace
return CGSize(width: remainingWidth/2, height: remainingWidth/2)
}

Are you building with Xcode 11 (GM 2)? I had similar issues implementing sizeForItemAt.
It seems something is causing the collection views to disregard the size returned in sizeForItemAt and instead use the size of the child views.
What worked for me is setting Estimate Size to None on the collection view in Interface Builder.
This is not ideal, especially if you have many cells, but it was the only fix I found unfortunately.

Related

The сonstraints do not work on an iPad with collection view

I am using a collection view in a storyboard and I need one ribbon with an image in the center of the screen. Constraints work fine on iPhone, but not iPad. How can this be done?
The collectionView cell size is not specified for different screen sizes.
Use UICollectionViewDelegateFlowLayout's method
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
https://developer.apple.com/documentation/uikit/uicollectionviewdelegateflowlayout/1617708-collectionview

How to fix CollectionView cell's size and make an auto layout regardless of the initial state of the Simulator?

The code to set the width and height of the collectionView's cell is as following:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width/6, height: collectionView.frame.width/5)
}
When the Simulator is vertical before running the scheme, then we get
and
But when the Simulator is horizontal before running the scheme, we get
and
The size of cells will change as the Simulator's initial state. I think the reason is that collectionView.frame.width depends on the Simulator's state.
How to fix the cell's size using auto layout like the code before regardless the ipad's initial state?
Best and easiest way to get through this is to make it a sqaure rather than a Rectangle . basically have the same value to width/height in my opinion . If it doesnt help you try to below and one small change to your code is to use the bounds instead of the frames .
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width/5, height: collectionView.bounds.width/5)
}
also if you could guess a size margin in betweens you could also reduce that bounds.width/5 -10
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let standardWidth = max(collectionView.frame.width, collectionView.frame.height)
return CGSize(width: standardWidth/5, height: standardWidth/5)
}
This simple solution works well.

Multiple Sized CollectionView Cell

I want to make multiple UICollectionViewCells of Various size like the image.
Use collectionViewFlowLayout and make a function inside collectionViewFlowLayout subclass and call that function from following function to give desired size for the cell
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize

Issue with displaying a long cell(like a rectangle) in a collection view

I want to display a collection view image which looks like so...
Here, as you can see the whole cell is of a rectangle shape with space for text and buttons below the image. I am not able to load such a 'rectangle' shaped cell in my collectioview. Any help would be appreciated.
Thanks in advance...:)
To set the size of item of a collectionview you can implement this UICollectionViewDelegateFlowLayout method
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 30, height: 100)
}
Your question is not very clear as it does not show your code or what result you got. But it is quiet simple
For customising cell size use below code and pass width and height accordingly
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: accordingly to your requirement, height: accordingly to your requirement)
}
Please check this link which can explain every bit in detail. And play with different value to get your required result.
Custom cell collectionview

How to resize cells for size class swift

I have a UICollectionView in my app. I want it to constrain so it only has two cells on each horizontal row. For iPhone classes I achieve this by setting the width so three cells can't fit in the same horizontal row. This seems to be a fix but I don't know how to resize the cell for iPads. I have heard to use:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
but I can't access this function. Does Swift no longer support this function anymore?
Question: Can I force my screen to only display 2 cells on each horizontal row?
You should first conform to protocol UICollectionViewDelegateFlowLayout
and then implement the method :
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.size.width * 0.5, height: 30)
}

Resources