AutoLayout pros: TableCell resized depending on inserted UIView?

Hi,


I'm stumped here. I have a settings screen made up of a UITabelView. Some settings are just simple cells and some I have as UIviews layed out in InterfaceDesiger and loaded from the xib.

Currently these views have hardcoded heights. So when I load the view I add it as a subview to the cells contentview and then set the cell height to the hardcoded values in heightForRowAtIndexPath.


Now I want all this to be adaptive. I have set up the UIViews and their content with all possible constraints but when I insert the view the cell and it's content view will not do anything by default.

I guess I need to programatically create the constrains that makes MY views width adjust to the table width and other constraints that will force the cell height follow the calculated height of MY cell.


Have anyone done something like this and could provide any thoughts or idéas?


Kind regards,

Jan

Answered by Janne in 409166022

I like it when I, just by writing the question, answers myself... 😀

Of course I have to programmatically add the constraints myself and after reading up on how, it was easily solved.

So if there is anyone finding this post in search for something similar, this is how I did it:


Constrained the top, left and right sides of the view I was inserting to the top, left and right sides to the contentView of the table cell.

Then constrained the bottom of the contentView to the bottom of the inserted view.

This makes the view follow the width of the table cell and layout its content regarding this constraints. And the height of the cell is now dependent on the intrinsic size of the inserted view.


Just make sure that the inserted view is set to use Autolayout and all the constraints within it adds up.


Thanks for listening.

Accepted Answer

I like it when I, just by writing the question, answers myself... 😀

Of course I have to programmatically add the constraints myself and after reading up on how, it was easily solved.

So if there is anyone finding this post in search for something similar, this is how I did it:


Constrained the top, left and right sides of the view I was inserting to the top, left and right sides to the contentView of the table cell.

Then constrained the bottom of the contentView to the bottom of the inserted view.

This makes the view follow the width of the table cell and layout its content regarding this constraints. And the height of the cell is now dependent on the intrinsic size of the inserted view.


Just make sure that the inserted view is set to use Autolayout and all the constraints within it adds up.


Thanks for listening.

Thanks for the feedback. Which shows the power of autolayout.


Don't forget to close the thread by marking your correct answer.

AutoLayout pros: TableCell resized depending on inserted UIView?
 
 
Q