UI Collection View Cells and Auto Layout - ios

I have a screen in interface builder that consists of a UI Collection View containing reusable cells. I've used Interface builder to add Auto Layout constraints to the UI Collection view and its scaling and moving about on each device like it should. How can I use the same method (control dragging) to auto layout the cells width and height? I can get constraint options via control dragging for the child objects in the cell to the cell but not from the cell to say the UI Collection View.
Any help greatly appreciated!
Thanks.

You don't. Collection views manage the placement of the cells within the view themselves, using their own rules. You can certainly use AutoLayout to determine the size and placement of the entire collection view, and you can use it for the internal layout of individual cells, but you can't use AutoLayout to size or position the cells within the collection view.

Related

Nested CollectionView with dynamic height based on content whilst using MagazineLayout and Carbon

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.

Can we make UIView height always resize with the height of UIStackView inside?

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.

Self-sizing UITableView embedded in UICollectionViewCell

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.

Auto Layout: Set height View depends on other height View

I need to implement AutoLayout in my layout. Below are some screenshots:
1.
2.
The case is :
I have a ShinobiDataGrid in View and a UITableView . First image is first condition, in first picture I want the view have height as height View Controller.
The second and third picture is when there are available TableView . I want the TableView always is at the bottom of View Controller and the View is have height depends of height Table View.
I read this, this and all of reference on that but it's not working for me.
Edit
I read some article about Auto Layout and I get this below:
1.
2.
But in first picture, the the UIView still have a distance with main View. I want it only have 3 or 4 point from bottom of main View like below:
Now, I have one constrain from UIView to the UITableView. I add one constrain from UIView to bottom main View with custom priority, constant and etc but it's not working.
1.I would suggest you to use UITableView with section header and footers , with no scroll. Add your views to UITableViewCell. Populate your UITableView with custom cells based on your requirements.
2.You can also use UIStackView for the same. Add all your views & table views to a vertical stack view in Interface Builder. Keep distribution as fill equally. First time hide your UITableView. Your view will cover complete then show / hide tableview when required and update stack view.
Constraint your views & table views with respect to stack view. UIStackView manages autolayout.
3.I would have used UITableViewController with static cells (with no scroll) with views and tableviews as subview of static UITableViewCells. First time in heightFoRowAtIndexPath pass complete height. When tableview will be visible. Reload tableview controller and pass height accordingly.
Instead of using UITableView inside cell. I would suggest using UIStackView as subview cell. Show your data in UIStackView.
First add scrollview.
Add the height constraint of First View equal to view controller.
Below to that, add the tableview, get the dynamic height and change according to it. You can see my answer here for this.

Can hard coded and self-sizing table cells be intermixed within a tableview?

I'm working on an app that will populate the tableview based on a web service response. So far the two content types I know I will be getting will be data shown in a textview, and also in a collection view. I've begun implementing Ash Furrow's AFCollectionView. The dynamic sizing for text areas is working, however the collection view is scrolling within a cell height that is even less than my estimatedRowHeight (210.0).
Because the collection view table cell is not correctly resizing is it possible to explicitly set the height for the collectionView cell(s), and let any of the text based cells remain dynamic?
Your self sizing cells use auto-layout to determine what height to be, and that's based on the intrinsic content size of the subviews. But, that's not the only way auto-layout can specify the height of a view. You can add a static height constraint to your cells which contain a collection view.

Resources