Here's a fairly basic question about current iOS development, to which I've never found a good answer.
In a (say) vertical UICollectionView
,
Is it possible to have full-width cells, but, allow the dynamic height to be controlled by autolayout?
(If you're new to iOS "dynamic height", meaning the cell has a few, say, text views which could be any length or images which could be different sizes, so ultimately each cell is a totally different height.)
If so how?
This strikes me as perhaps the "most important question in iOS with no really good answer."
Not sure if this qualifies as a "really good answer", but it's what I'm using to accomplish this. My flow layout is horizontal, and I'm trying to make the width adjust with autolayout, so it's similar to your situation.
Then in the view controller where the autolayout constraint gets modified, I fire off an NSNotification.
In my UICollectionView subclass, I listen for that notification:
and invalidate the layout:
This causes
sizeForItemAt
to be called again using the collection view's new size. In your case, it should be able to update given the new constraints available in the layout.There are couple of ways you could tackle this problem.
One way is you can give the collection view flow layout an estimated size and calculating the cell size.
Note: As mentioned in the comments below, as of iOS 10 you no longer need to provide and estimated size to trigger the call to a cells
func preferredLayoutAttributesFitting(_ layoutAttributes:)
. Previously (iOS 9) would require you to provide an estimated size if you wanted to query a cells prefferedLayoutAttributes.(assuming you are using storyboards and the collection view is connected via IB)
For simple cells that will usually be enough. If size is still incorrect, in the collection view cell you can overridefunc preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes
, which will give you more fine grain control over the cell size. Note: You will still need to give the flow layout an estimated size.Then override
func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes
to return the correct size.Alternatively, instead you can use a sizing cell to calculate the size of the cell in the
UICollectionViewDelegateFlowLayout
.While these are not perfect production ready examples, they should get you started in the right direction. I can not say this is the best practice, but this works for me, even with fairly complex cells containing multiple labels, that may or may not wrap to multiple lines.
None of the solutions were working for me as I need dynamic width to adapt between iPhones width.
Personally I found the best ways to have a UICollectionView where AutoLayout determines the size while each Cell can have a different size is to implement the UICollectionViewDelegateFlowLayout sizeForItemAtIndexPath function while using an actual Cell to measure the size.
I talked about this in one of my blog posts: https://janthielemann.de/ios-development/self-sizing-uicollectionviewcells-ios-10-swift-3/
Hopefully this one will help you to achieve what you want. I'm not 100% sure but I believe unlike UITableView where you can actually have a fully automatic height of cells by using AutoLayout inconjunction with
UICollectionView does not have such a way of letting AutoLayout determine the size because UICollectionViewCell does not necessarily fills the whole width of the screen.
But here is a question for you: If you need full screen width cells, why do you even bother using the UICollectionView over a good old UITableView which comes with the auto sizing cells?
On your
viewDidLayoutSubviews
, set theestimatedItemSize
to full width (layout refers to the UICollectionViewFlowLayout object):On your cell, be sure that your constraints touch both the top and bottom of the cell (the following code uses Cartography to simplify setting the constraints but you can do it with NSLayoutConstraint or IB if you want):
Voila, you cells will now autogrow in height!
I found a pretty easy solution for that issue: Inside of my CollectionViewCell I got a UIView() which is actually just a background. To get full width I just set the following Anchors
The "magic" happens in the first line. I set the widthAnchor dynamically to the width of the screen. Also important is to subtract the insets of your CollectionView. Otherwise the cell won't show up. If you don't want to have such a background view, just make it invisible.
The FlowLayout uses the following settings
Result is a full width sized cell with dynamic height.