UICollectionView spacing between rows becomes huge on certain devices - ios

I have a collection view with the minimum spacing set to 10 for cells and lines. The horizontal space between the cells adjusts correctly, but I don't know how to alter the vertical spacing between the rows of cells. On the 6, which it was originally developed on, the space is negligible and works fine. But on every other device, the space quadruples in size for no apparent reason. Is there any way to set the spacing between the rows of cells to be a constant value across all devices?

Right now there isn't a way to set the size of collection view cells using AutoLayout. My guess is that the size of your cells just doesn't look good on other screen sizes, so you most likely will need to implement the UICollectionViewDelegateFlowLayout protocol (which extends the UICollectionViewDelegate protocol) and the func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize method.
Although if that's not the case maybe posting some screenshots would help.
Example method:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if(self.view.bounds.width > 400){
return CGSize(width:self.view.bounds.width/3-10, height:self.view.bounds.width/3-10)
}
return CGSize(width:self.view.bounds.width/2-10, height:self.view.bounds.width/2-10)
}

Related

How to make rounded collection cells fit on all screens?

So I'm normally use to making square sized collection view cells fit perfectly on all screens. However once I start adding spacing and section insets I run into this problem. I am also using the storyboard.
This is how I would like all of the cells to look on each screen.
iphoneX
I have the minimum space for cells and lines at 10.
And for the right and left section insets they're set to 10 too.
This is the result I get on other devices ( iPhone 7/XsMax)
You have to set cell size programmatically by implementing func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize method which is in UICollectionViewDelegateFlowLayout protocol.
In this method you can set collection view cell's width
(UIScreen.main.bounds.size.width/2)-10

Collection View Cell Size/Fitting issue

I am working on an iOS app in swift 4. I have implemented collection view and there are two cells at the front as shown in the image:
When I run my app on bigger screen sizes like iPhone 6/7/8 Plus, then it Shows two complete cells with one-half cell. What I want to achieve is that it shows two cells in front of every screen size, Please help.
Implement the UICollectionViewDelegateFlowLayout protocol in your ViewController, then implement:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let cellSize = //calculate cell size based on the width of the view and padding between cells
return cellSize
}
Check out the documentation for the UICollectionViewDelegateFlowLayout and implement its methods to modify the spacing between the cells.

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

How to force layout of collection view without compromising on the performance?

Here's my problem. I have a UICollectionViewCell having an image View. The image is downloaded from a link. The images are of different sizes and I want the collectionView cell size to change with the image size. I am currently accomplishing this by forcing the layout inside
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath)
by placing this line of code in it
collectionViewTrending.reloadItemsAtIndexPaths([indexPath])
The below UICollectionViewDelegate function
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath)
is handling the resizing of the cell.
Though this is working but it definitely is NOT efficient. It is inefficient because scrolling on the collectionView is taking longer and it is not smooth. Is there any way in which I can resize the cell without degrading the performance?
Remove that line--collectionViewTrending.reloadItemsAtIndexPaths([indexPath]) first..
Then set sizeForItem method of collectionView properly..It will work fine..

How to make UICollectionViewCell's width auto resize and its height stay the same

I want my custom UICollectionViewCell's width dynamically changes depending on the UILabel content inside, but each cell should stay the same height. Using estimatedItemSize will also change the height value (which isn't what I want, I need it to stay the same, say 30.)
And after reloading data for several times it starts being like an chaos, making some cells not vertical align.
Did you try implementing
collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
Finally I solved the problem by using sizeWithAttributes in sizeForItemAtIndexPath to dynamically determine the width of each cell should be.

Resources