Is there any existing flow layout which can support a collection view which the user can scroll a collection of images in a horizontal direction but have the images have variable width and same height? I have one sort of working but it sometimes randomly spaces the images. Thanks.
Related
I am creating a CollectionView where the cell has a nested CollectionView. I am using Carbon to display the cells in the collection view and using MagazineLayout for the layout. There is only one Component which holds two views, a label and another CollectionView both inside of a vertical stack view with top, left, bottom and right constraints. Here is a picture of what the expected layout should be and it is similar to a "thread" of messages where a reply adds a new thread and so on. The nesting gets larger with more threads:
At the moment the label displays correctly but the CollectionView height does not grow dynamically based on the cells inside of the CollectionView as shown below.
I have tried using this example which subclasses UICollectionView and overrides layoutSubviews and intrinsicContentSize. This works to a point apart from the layout glitches and some of the nested CollectionViews heights change when scrolling.
I have also looked into using UICollectionViewFlowLayout as the EstimatedItemSize can be set which does most of the heavy lifting with setting the height dynamically. However, this solution cannot be used as MagazineLayout is a subclass of UICollectionViewLayout.
I can send an example project or more information if needed. Thank you for any help.
After seeing the wonderful Advances in Collection View Layout Session, I wondered if is possible to use the capabilities implemented in the new Collection View Compositional Layouts framework to create a layout based on previously unknown cell sizes.
My problem:
Imagine a horizontal image gallery with two levels, the photos can be "vertical" or "horizontal", and the app is fetching them when are needed for display, and they are provided randomly ("horizontally" or "vertically"). So we have to populate the gallery with a "random pattern" (Pinterest style, here is how to do it with UICollectionViewFlowLayout).
I cannot see a "group" pattern to populate the cells, because the layout has infinite possibilities, vertical divisions can appear to create groups, but also can not.
Is it possible to archive with Collection View Compositional Layouts?
Thank you.
When you're setting your item layout size, you can use an estimated layout dimension to make the size dependent on its contents. So an image view can set the cell size from the inside out using constraints. You size the image when you populate the cell, depending on whether it is horizontal or vertical, and the image view adopts the size of the image and the cell adopts the size of the image view.
(If you needed even more control you could construct your group as a NSCollectionLayoutGroup.custom, which allows you set every item's frame manually. But there's no need for such extremes in this simple situation, and besides, that assumes you know the frame at layout declaration time, which you probably don't.)
I am trying to create the layout in the image
Here is the main requirements:
The content in each section is data driven and retrieved from the api.
The height of each section is dynamic, based on how many fields we have.
The scene height is dynamic.
The scene is vertically scrollable.
Here is what I tried to do:
- Using static cell UITableView
- Using stack view with a scroll view
In both cases I got stuck with dealing with stack views for the dynamic data fields, and I struggled in handling the dynamic height for each section.
Any Ideas?
Your requirement is pretty straight forward and can be met using a UITableView. Use dynamic UITableViewCell. The reason i choose dynamic table view cell is that you can resize the cell height according to content. Measure your content size inside the tableView:heightForRowAtIndexPath:.
The flow will be following:
Initiate the api call
While the data isn't available return the cell height according to your placeholder view (i.e placeholder image height)
While the data is available, process your data and reload the table view
After reloading the table view, table view will ask each cell's height once again, so this is the chance you get to calculate the content height. (I assume that you can measure the string height while the string width and font is given. Plz google that, thats obvious.)
return that calculated height.
Optimization Possibilities:
Instead of recalculating the cell height each time, you can calculate the cell height once and store that height inside a simple dictionary keyed with IndexPath.
I'm trying to implement an iOS collection view which basically should work similar to a table view, but the cells' widths being integer multiples of the collection view's width (eg. 4 times the width). the collection view should scroll vertically (like a regular tableview does), but it should page horizontally across the content.
I do have the custom layout working except for the paging. Currently, the collectionView will just scroll horizontally. I'm unsure how to implement the paging correctly.
If I just set pagingEnabled on the collectionView, it has no effect. I suspect that the scrollview needs to be told what the width of a page is..?
Or do I have to do anything weird because there is only one cell/column per row, instead of multiple cells?
See the image; the grey bars are the cells, the blue rect is the collection view frame.
thanks!
I believe if the collection view item widths are less than or equal to the width of the collection view, then the horizontal paging should work via the pagingEnabled property. Otherwise, you need to create the paging effect yourself using the UIScrollView delegate methods and animating to offsets yourself after a certain threshold
The problem I am forcing is:
I have 1 big scrollView and I have to add to it 5 subviews programmatically
when I don't know what heights the sub views will have :(
Is there some API to do so ?
or I have to count the subviews heights manually and add them with CGRectMake
based by calculated sizes?
What I would do is use Auto Layout. If you add your subviews to the scroll view using internal constraints, then these constraints can determine not only the distance of the subviews from one another but also the distance from the imaginary content view that surrounds them. The scroll view then uses the size of the content view as its contentSize, and the whole thing becomes scrollable. And this works despite the fact that you don't know the heights of any of the views, which is exactly what you're after.