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
Related
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
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.
Normally I just use this function:
optional func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat
to set some spacing between the items in a collectionView, but the spacing is the same for all items. I would like to set the spacing to be different between some items, depending on some logic that I need to check, the logic will depend on which cell is before and after, so I would need something like IndexPath to check that.
When we want to set size of a cell, we conveniently get IndexPath from the function below, but not from the function above.
optional func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
What can I do to achieve this?
There's no good way to do it without creating your own layout. There's too much to it to go into it here, but there's tons of good tutorials on the matter. Here's a good place to start.
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
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)
}