I have an array of stack views with varying heights:
[Stack View A, Stack View B, Stack View C, Stack View D]
I want to display them in a collectionView. Every stackView goes into a different collectionView cell. I am currently using a flow layout to set the height based on what the view is in the collection viewController.
Would it be possible to abstract this so that the collection vc doesn't need to know anything about them? I can pass the view to be displayed in the cell and the cell's height is determined by the height of the stack view. Something like intrinsic content size, as opposed to setting it in the flow layout delegate sizeforitem method.
You could set the Estimated Size of the collectionView as "Automatic" as well as the cell's size and each cell will self size with Auto Layout constrained views.
As stated in the Xcode 11 Release Notes:
Cells in a UICollectionView can now self size with Auto Layout
constrained views in the canvas. To opt into the behavior for existing
collection views, enable “Automatic” for the collection view’s
estimated size, and “Automatic” for cell’s size from the Size
inspector. If deploying before iOS 13, you can activate self sizing
collection view cells by calling performBatchUpdates(_:completion:)
during viewDidLoad(). (45617083)
Related
I have a content view (this content view I will use as a subclass of UICollectionViewCell content later) which has an UIStackView inside. Inside that stack view, I add some labels, view. And I calculate some conditions to make sure that when data of a label is empty, that label will be hidden and the stack view will auto change the height and vice versa, if they have data I'll show them and stack view update the height again.
Now I wanna know how can I make the content view height always follow the UIStackView height whenever it's changing.
If you already made your UIStackView to have a dynamic height following its children, then all you need is to add constraints between UIStackView and your content view and it will resize automatically as well.
Now, if you add that content view inside a UICollectionViewCell, then you must make sure to call UICollectionView.reloadData() whenever you change the content of your UIStackView. That way the collection view will recalculate the size and render the cell again. You also have the option to reload a single cell in the collection view if you have a way to determine its indexPath.
Note: Take care of CollectionViewFlowLayout and what size it dictates to the cell. It's recommended that you tell the flow layout the estimated height of your cell via layout.estimatedHeight.
Main question:
How can I easily setup a UI Flow Layout of fixed size items/cells so the parent container view (or Table Cell content) grows height to fit? The flow layout should therefore not scroll its own items. Currently my parent view scrolls (see the screenshots). What is the correct constraints setup for the height of the parent view to the UICollectionView (with flow layout). What is needed in the code to allow the height of the parent view to match its child items height?
Details:
I am trying to create an CollectionView inside a TableCell (or any parent content view) so that:
- Parent view or table cell's height will resize to the content of the CollectionView
- The CollectionView uses Flowlayout with multiple fixed size items (i.e. 50x50) that wrap (and therefore grow the height.
Currently, the content of the flowlayout does wrap however I the parent content view always scrolls. I would like the parent view, or TableCell with a content view to automatically size to the CollectionView height. For example - one TableCell may have 3 rows of rectangles 50x50 and the parent view height would be 150. Another row may have 1 row and parent view height be 50.
Main List Screen (Table View with Collection List in each row - items can be scrolled but the table cell doesn't resize height to fit them
storyboard setup for main list screen of items in table view
Example of My Awards of flowlayout items in a parent content view which doesn't fit the child items height
Thank you
For an iOS application I'm working on the following layout:
I'm trying to achieve this by embedding a tableview inside a collection view cell.
The height of the individual collectionView cells is dynamic (by setting the layouts' estimatedItemSize and using auto layout for the cells). The problem which I encountered is that I can't get the embedded tableView to size dynamically according to the given data.
Is there any way I can update the size of the tableView in the cell dynamically
For collection view or table view, auto-sizing will only work if you have provided enough constraint to calculate it's CGRect. In your case, you have table view inside collection view and table view's height can be anything as it can scroll the content.
Try to give table view height constraint then change constraint's value to table view's contentsize.height, Then it might work.
Maybe consider using UIStackView.
I have a side-scrolling collection view that contains two images. These images may be changed by the user during runtime. This change might cause a change of the collection view cell size, hence the collection view also need to adapt its height according to the height of the cells.
I've implemented the UICollectionViewDelegateFlowLayout protocol and the sizeForItemAt function which returns the proper cell size given the image.
I also have a height constraint outlet connected to my collection view which allows me to change its height, matching the height value of the chosen image.
It is all presented as you would expect when drawn to the screen, but I get the error "The behavior of the UICollectionViewFlowLayout is not defined because..", complaining that the height returned by sizeForItemAt exceeds my collection views default height (set in the storyboard).
It seems that I need to invalidate my collection view layout once I've altered its height constraint so that the collection view layout is updated with this new value before sizeForItemAt returns the cell sizes.
I've tried to call UICollectionViewLayouts invalidateLayout() right after updating the collection view height constraint, but that didn't help.
Any ideas to what I am doing wrong?
SOLVED
Ok, what I needed to do was to call view.layoutIfNeeded() for my view controller that holds the collection view right after I changed the collection views height constraint. Then the bounds for the collection view will be updated with the new height value so that the cells will fit.
My UITableViewCell subclass has several UILabels in it of varying length, and with iOS 8 and Auto Layout constraints in the cell the height is now automatically configured and cell heights can be dynamic.
However I want to embed all the contents of the cell in another UIView. Is it possible for the cell's height to still be dynamic? As in the UIView that the cell contents are embedded in will grow and shrink automatically based on the content and thus the cell will as well? Is this possible?
Dynamic height works by performing an Autolayout pass on the cell's content view. As long as the constraints are in place for a height to be calculated, the specific view hierarchy should be irrelevant.