简体   繁体   中英

Auto layout: auto update constraints when some view is empty

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM