简体   繁体   English

自动布局:当某些视图为空时自动更新约束

[英]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 X+1与底部Label X Label 0 is constrained to the superview's top with a fixed constant. Label 0以固定常量约束到superview的顶部。

What I'm trying to achieve is: if some Label X is empty, then Label X+1 has to take its place. 我想要实现的是:如果某些Label X为空,那么Label X+1必须取而代之。

 _______________________________
|     |                         |
|  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. 如果不这样做:您可以在viewWillAppear方法中进行更新。 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. 将行数设置为0可让标签根据需要动态使用多行。 If this is an empty label that should actually become 0 lines. 如果这是一个空标签,实际上应该变成0行。 This will update the frames and autolayout should take care of the rest. 这将更新帧,自动布局应该照顾其余部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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