如何使用自动布局当一个视图被隐藏到移动其他的意见? [英] How to use auto-layout to move other views when a view is hidden?

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

问题描述

我已经设计在IB我的自定义单元格,它的子类并连接我的网点到我的自定义类。我在单元格内容中提供三子视图分别是:的UIView(cdView)和两个标签(titleLabel和emailLabel)。根据现有的每一行数据,有时候我想拥有的UIView和两个标签显示在我的牢房,有时只有两个标签。我所试图做的是设置约束的方式,如果我设置的UIView属性为隐藏,否则我将在上海华删除两个标签会向左移动。我试图主导约束集的UIView为上海华(单元格内容)为10像素10px的和UILabels导致约束下视图(UIView的)。后来在我的code

   - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(IndexPath *)indexPath {
...
记录*记录= [self.records objectAtIndex:indexPath.row];如果([record.imageURL是equalToString:@]){
     cell.cdView.hidden = YES;
}

我躲在我的cell.cdView,我想的标签移到左边但他们是住在小区相同的位置。我试图从上海华删除cell.cdView,但它也不能工作。我附上图片,以澄清我什么。

我知道如何编程做到这一点,我不是寻找一个解决方案。我想是设置在IB的限制,我希望,如果其他意见删除或隐藏我的子视图将动态地移动。是否有可能与自动布局为此在IB?

  .....


解决方案

这是可能的,但你必须做一些额外的工作。有几个概念性的东西走出的第一个方式:


  • 隐藏的意见,即使他们不画,仍然参与自动布局并通常保留其帧,留下其他相关意见,他们的地方。

  • 当从它的父删除视图,所有相关的约束也从该视图层次结构中移除。

在你的情况,这可能意味着:


  • 如果您设置左视图被隐藏,标签留在地方,因为这左视图仍然占用空间(即使它不可见)。

  • 如果您删除左视图,您的标签可能会被留下含糊的限制,因为你再也不用为标签的左边缘的限制。

您需要做的是明智的过度约束标签。离开你现有的约束(10pts空间,另一种观点)独自一人,但另增加一约束:与非必需优先让你的标签的左边缘10pts从上海华的左边缘走(默认高优先级可能会工作得很好)

然后,当你希望他们向左移动,完全消除左视图。强制性10PT制约左视图将它涉及到视图一起消失了,你会只是一个高优先级的约束留下的标签上10pts从上海华程。在接下来的布局传递,这应该会使他们,直到他们填补了上海华的但对周围的边缘间距的宽度扩大左侧。

一个重要的警告:如果你想你的左视图回的图片,不仅你有重新添加到视图层次,但你也必须为重新建立所有的约束条件是在同一时间。这意味着你需要一种方法来把视图和标签之间的间距10PT回的约束,只要这一观点再次显示。

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;
}

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.

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:

  • If you set your left view to be hidden, the labels stay in place, since that left view is still taking up space (even though it's not visible).
  • If you remove your left view, your labels will probably be left ambiguously constrained, since you no longer have constraints for your labels' left edges.

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).

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.

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天全站免登陆