隐藏视图时如何使用自动布局移动其他视图? [英] How to use Auto Layout to move other views when a view is hidden?

查看:26
本文介绍了隐藏视图时如何使用自动布局移动其他视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 IB 中设计了我的自定义 Cell,将其子类化并将我的插座连接到我的自定义类.我在单元格内容中有三个子视图:UIView (cdView) 和两个标签(titleLabel 和 emailLabel).根据每行可用的数据,有时我希望在我的单元格中显示 UIView 和两个标签,有时只显示两个标签.如果我将 UIView 属性设置为 hidden 或者我将从超级视图中删除它,我想要做的是设置约束,这两个标签将向左移动.我尝试将 UIView 前导约束设置为 10px 的 Superview(单元格内容),并将 UILabels 前导约束设置为 10 px 到下一个视图(UIView).稍后在我的代码中

I have designed my custom Cell in IB, subclassed it and connected my outlets to my custom class. I have three subviews in cell content which are: UIView (cdView) and two labels (titleLabel and emailLabel). Depending on data available for each row, sometimes I want to have UIView and two labels displayed in my cell and sometimes only two labels. What I am trying to do is to set constraints that way if I set UIView property to hidden or I will remove it from superview the two labels will move to the left. I tried to set UIView leading constraint to Superview (Cell content) for 10px and UILabels leading Constraints for 10 px to the next view (UIView). Later in my code

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(IndexPath *)indexPath {
    
    // ...

    Record *record = [self.records objectAtIndex:indexPath.row];
    
    if ([record.imageURL is equalToString:@""]) {
         cell.cdView.hidden = YES;
    }
}

我正在隐藏我的 cell.cdView 并且我希望标签向左移动,但是它们在 Cell 中保持在相同的位置.我试图从超级视图中删除 cell.cdView 但它也不起作用.我附上了图片来阐明我的意思.

I am hiding my cell.cdView and I would like the labels to move to the left however they are staying in the same position in Cell. I tried to remove cell.cdView from superview but it didn't work either. I have attached image to clarify what I am about.

我知道如何以编程方式执行此操作,我不是在寻找该解决方案.我想要的是在 IB 中设置约束,如果其他视图被删除或隐藏,我希望我的子视图会动态移动.是否可以通过自动布局在 IB 中执行此操作?

I know how to do this programatically and I am not looking for that solution. What I want is to set constraints in IB and I expect that my subviews will move dynamically if other views are removed or hidden. Is it possible to do this in IB with auto-layout?

.....

推荐答案

这是可能的,但您必须做一些额外的工作.有几个概念性的事情要先让开:

It is possible, but you'll have to do a little extra work. There are a couple conceptual things to get out of the way first:

  • 隐藏的视图,即使它们不绘制,仍然参与自动布局并且通常保留它们的框架,将其他相关的视图留在它们的位置.
  • 从其父视图中删除视图时,所有相关约束也将从该视图层次结构中删除.
  • Hidden views, even though they don't draw, still participate in Auto Layout and usually retain their frames, leaving other related views in their places.
  • When removing a view from its superview, all related constraints are also removed from that view hierarchy.

就您而言,这可能意味着:

In your case, this likely means:

  • 如果您将左视图设置为隐藏,标签将保持原位,因为该左视图仍在占用空间(即使它不可见).
  • 如果您移除左视图,您的标签可能会受到不明确的约束,因为您不再对标签的左边缘有约束.

您需要做的是明智地过度限制您的标签.保留您现有的约束(另一个视图的 10pts 空间),但添加另一个约束:使标签的左边缘距其超级视图的左边缘 10pts 且具有非必需的优先级(默认的高优先级可能会很好地工作).

What you need to do is judiciously over-constrain your labels. Leave your existing constraints (10pts space to the other view) alone, but add another constraint: make your labels' left edges 10pts away from their superview's left edge with a non-required priority (the default high priority will probably work well).

然后,当您希望它们向左移动时,请完全移除左视图.左视图的强制性 10pt 约束将与它相关的视图一起消失,您将只剩下一个高优先级约束,即标签距其超视图 10pts.在下一次布局过程中,这应该会导致它们向左扩展,直到它们填满超级视图的宽度,但要保留边缘周围的间距.

Then, when you want them to move left, remove the left view altogether. The mandatory 10pt constraint to the left view will disappear along with the view it relates to, and you'll be left with just a high-priority constraint that the labels be 10pts away from their superview. On the next layout pass, this should cause them to expand left until they fill the width of the superview but for your spacing around the edges.

一个重要的警告:如果你想让你的左视图回到图片中,你不仅必须将它添加回视图层次结构,而且你还必须重新建立它的所有约束在同时.这意味着您需要一种方法来在再次显示该视图时将视图与其标签之间的 10pt 间距约束放回原处.

One important caveat: if you ever want your left view back in the picture, not only do you have to add it back into the view hierarchy, but you also have to reestablish all its constraints at the same time. This means you need a way to put your 10pt spacing constraint between the view and its labels back whenever that view is shown again.

这篇关于隐藏视图时如何使用自动布局移动其他视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆