Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolayout how to support the substitution of a subview?

My problem is related to the use of autolayout (the famous) ;)

I would like to manage 2 situations of subviews in the same view controller so that I can go from one situation to another (and reciprocally).

Let me introduce the problem with the following diagram :

Diagram of the same view controller in the 2 situations

The problem is that I can't figure how to express the autolayout constraints in Interface Builder so that I can easily switch from one situation to another. To be more precise the subviews VIEW_B1 and VIEW_B2 are differents subviews with different height.

I already try to hide the subview I don't want to see (either VIEW_B1 or VIEW_B2) but in this case the VIEW_C will remains at its Y position. What I'd like it's VIEW_C to be glue to the VIEW_B* (the one that is not hidden).

like image 947
helper Avatar asked Nov 02 '22 21:11

helper


1 Answers

Hiding the view doesn't remove the constraints. You have to remove view B1 from its superview, insert B2 and then add constraints pinning B2 to the views above and below it.

If you don't want to remove the view altogether, you can store the constraints linking each B* view to the views above and below it in properties, and then when you switch out the views, remove and add the appropriate constraints to the superview.

like image 136
jrturton Avatar answered Nov 08 '22 10:11

jrturton