Vertically aligned Collection view cells with varying heights - ios

I am building a collection view, every cell has a fixed width but a varying height. There should be a border of 1 px within each cell. I have tried to find a way to make them vertically align but end up with white space in between them. Also some times the cells end up on top of each other.
What would be the best wat to solve this problem. I have considered subclassing UICollectionViewFlowLayout
I am currently setting the size like this:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: CGFloat(defaultCellSize), height: CGFloat(defaultCellSize * ratio))
}

Try using this library https://github.com/chiahsien/CHTCollectionViewWaterfallLayout It has handled what is required.

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

Dynamic Buttons in UIScrollView with Equal width and height

I want to display list of buttons in scrollview with dynamic height based on screen size. I can able to do this in UIView, but when I applied same auto layout approach in UIScrollView, it's not increasing the size of buttons. So please guide me How to increase the button size in UIScrollView based on screen size.
if button is dynamic and may be increase and decrease in future than you Should use CollectionView not ScrollView.
Drag CollectionView into ViewController.
Drag Cell in CollectionView.
Add button(or label based on your requirement) In Cell.set button(leading,trailing,top,bottom) == Cell(leading,trailing,top,bottom)
Write dataSource and delegate methods and UICollectionViewDelegateFlowLayout methods.
and change Following Methods to increase button height According To Screen
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: self.view.bounds.width/2, height: self.view.bounds.height*0.15)
}

CollectionView rows Side By Side - should have same dynamic height

On the IPad I have two different UICollectionViewCells side by side like two columns. The first one have a width of 30% the second of 70%.
This two cells get dynamic content and have different heights. At the moment I return some static height in
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { ... }
This height is always too large.
So I want that the rows have just one height and this height is the height of the row with more content.
Is there a method how I can calculate how much height a UICollectionViewCell needs? If not, which possibilities do I have?
I tried to generate the height by content like this
for item in contentInARow {
contentHeight += 40
}
But this 40 is too static because the content could be too big or small.
I'm thankful for some input.

Swift auto layout in UICollectionView with intrinsic height of cells

I'm having a hard time with auto layout and UICollectionView. I have a vertical UICollectionVIewFlowLayout that contains two different types of cells, as in the image below:
The SingleCard cell contains a subview with an image (UIImageView). The HorizList cell contains a subview which is a horizontal UICollectionView (a list of thumbnail photos). The heights of the two cell types are as follows:
SingleCard: the width of the cell shall be the same as the screen width. The height shall be dynamic to the height of the UIImageView (to keep proportions of the image and have it fill screen width no matter of device size)
HorizList: the height is constant, say 200 points.
My attempt, in the CollectionViewController is to do
override func viewDidLoad() {
...
let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
layout.estimatedItemSize = CGSize(width: 200.0, height: 235.0)
}
This I do to get the two cell's intrinsic size to apply. Then I do not implement
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
at all.
My question is, what do I implement in
override var intrinsicContentSize: CGSize {
??
}
for the two different cell types? Maybe this is not the only thing I need to do, if so I'm grateful for additional hints.
Thanks!
/ola

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