Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto layout: auto update constraints when some view is empty

Tags:

ios

autolayout

I have a view like this

 _______________________________
|     |                         |
|  Label 0                      |
|     ^                         |
|  Label 1                      |
|     ^                         |
|  Label 2                      |
|     ^                         |
|  Label N                      |
|_______________________________|

^ stands for a constraint that links the top of Label X+1 with the bottom of Label X. Label 0 is constrained to the superview's top with a fixed constant.

What I'm trying to achieve is: if some Label X is empty, then Label X+1 has to take its place.

 _______________________________
|     |                         |
|  Label 0 (empty)              |
|     ^                         |
|  Label 1                      |
|     ^                         |
|  Label 2                      |
|     ^                         |
|  Label N                      |
|_______________________________|
               |
               |   The new layout
              \ /
 _______________________________
|     |                         |
|  Label 1                      |  (here it is also Label 0 but is empty)
|     ^                         |
|  Label 2                      |
|     ^                         |
|  Label N                      |
|_______________________________|

Is this possible with auto layout or do I have to programatically check emptiness to update the constraints manually?

Disclaimer: If this questions sounds very basic excuse me, please, I'm migrating from springs and struts and it is a quite criptic for now.

like image 963
emenegro Avatar asked Nov 03 '22 19:11

emenegro


1 Answers

Does it need to change when content is visible? If not try this: you could do an update in the viewWillAppear method. For each label, call

[firstLabel setNumberOfLines:0];
 [firstLabel setText:newProductTitleText];
 [firstLabel sizeToFit];

Setting number of lines to 0 lets the label dynamically use an many lines as it needs. If this is an empty label that should actually become 0 lines. This will update the frames and autolayout should take care of the rest.

like image 63
bkbeachlabs Avatar answered Nov 11 '22 15:11

bkbeachlabs