本文翻译自:How to use auto-layout to move other views when a view is hidden?

I have designed my custom Cell in IB, subclassed it and connected my outlets to my custom class. 我在IB中设计了自定义Cell,将其子类化并将我的插座连接到我的自定义类。 I have three subviews in cell content which are: UIView (cdView) and two labels (titleLabel and emailLabel). 我在单元格内容中有三个子视图:UIView(cdView)和两个标签(titleLabel和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. 根据每行可用的数据,有时我想在我的单元格中显示UIView和两个标签,有时只有两个标签。 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. 我想要做的是设置约束,如果我将UIView属性设置为隐藏或我将从superview中删除它,两个标签将移动到左侧。 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). 我尝试将UIView前导约束设置为10px的Superview(单元格内容)和UILabels将10 px的约束引导到下一个视图(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. 我隐藏了我的cell.cdView,我希望标签向左移动,但是它们在Cell中保持相同的位置。 I tried to remove cell.cdView from superview but it didn't work either. 我试图从superview中删除cell.cdView,但它也没有用。 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. 我想要的是在IB中设置约束,并且如果删除或隐藏其他视图,我希望我的子视图将动态移动。 Is it possible to do this in IB with auto-layout? 是否可以在具有自动布局的IB中执行此操作?

.....

#1楼

参考:https://stackoom.com/question/1Dnm6/如何在隐藏视图时使用自动布局移动其他视图


#2楼

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). 单独留下现有约束(10pts空间到另一个视图),但添加另一个约束:使标签的左边缘与超级视图的左边缘相差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. 左视图的强制10pt约束将与其相关的视图一起消失,并且您将只留下一个高优先级约束,标签距其超视图10pts。 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. 这意味着只要再次显示该视图,您就需要一种方法将视图及其标签之间的10pt间距约束放回去。


#3楼

将uiview和标签之间的约束连接为IBOutlet,并在设置hidden = YES时将优先级成员设置为较小的值


#4楼

Adding or removing constraints during runtime is a heavyweight operation that can affect performance. 在运行时添加或删除约束是一项可能影响性能的重量级操作。 However, there is a simpler alternative. 但是,有一个更简单的选择。

For the view you wish to hide, set up a width constraint. 对于要隐藏的视图,请设置宽度约束。 Constrain the other views with a leading horizontal gap to that view. 使用与该视图的前导水平间隙约束其他视图。

To hide, update the .constant of the width constraint to 0.f. 要隐藏, .constant宽度约束的.constant更新为0.f. The other views will automatically move left to assume position. 其他视图将自动向左移动以占据位置。

See my other answer here for more details: 有关详细信息,请参阅我的其他答案:

How to change label constraints during runtime? 如何在运行时更改标签约束?


#5楼

For the Googlers: building on Max's answer, to solve the padding issue that many have noticed I simply increased the height of the label and used that height as the separator instead of actual padding. 对于Google员工:在Max的答案基础上,为了解决许多人注意到的填充问题,我只是增加了标签的高度,并将该高度用作分隔符而不是实际的填充。 This idea could be expanded for any scenario with containing views. 对于包含视图的任何场景,可以扩展此想法。

Here's a simple example: 这是一个简单的例子:

In this case, I map the height of the Author label to an appropriate IBOutlet : 在这种情况下,我将Author标签的高度映射到适当的IBOutlet

@property (retain, nonatomic) IBOutlet NSLayoutConstraint* authorLabelHeight;

and when I set the height of the constraint to 0.0f , we preserve the "padding", because the Play button's height allows for it. 当我将约束的高度设置为0.0f ,我们保留“填充”,因为“ 播放”按钮的高度允许它。


#6楼

What I ended up doing was creating 2 xibs. 我最终做的是创建2个xib。 One with the left view and one without it. 一个是左视图,一个没有它。 I registered both in the controller and then decided which to use during cellForRowAtIndexPath. 我在控制器中注册了两个,然后决定在cellForRowAtIndexPath期间使用哪个。

They use the same UITableViewCell class. 它们使用相同的UITableViewCell类。 The downside is that there is some duplication of the content between the xibs, but these cells are pretty basic. 缺点是xib之间存在一些重复内容,但这些单元格非常基本。 The upside is that I don't have a bunch of code to manually manage removing view, updating constraints, etc. 好处是我没有一堆代码来手动管理删除视图,更新约束等。

In general, this is probably a better solution since they are technically different layouts and therefore should have different xibs. 一般来说,这可能是一个更好的解决方案,因为它们在技术上是不同的布局,因此应该有不同的xib。

[self.table registerNib:[UINib nibWithNibName:@"TrackCell" bundle:nil] forCellReuseIdentifier:@"TrackCell"];
[self.table registerNib:[UINib nibWithNibName:@"TrackCellNoImage" bundle:nil] forCellReuseIdentifier:@"TrackCellNoImage"];TrackCell *cell = [tableView dequeueReusableCellWithIdentifier:(appDelegate.showImages ? @"TrackCell" : @"TrackCellNoImage") forIndexPath:indexPath];

如何在隐藏视图时使用自动布局移动其他视图?相关推荐

  1. 创建视图时--ora-01731:出现循环的视图定义

    环境:oracle 11g 1.@ZFJW这个库下不存在V_JW_JSKCAP这个表,你在本库中定义的名字是:V_JW_JSKCAP 2.V_JW_JSKCAP @V_JW_JSKCAP;------ ...

  2. ssl服务器无响应,iOS上的IBM AppCenter 6.2.0 + SSL - 连接服务器时显示永久加载视图(无响应失败/成功/超时))...

    我试图连接iOS 6/8.1上的IBM AppCenter 6.2.0.01.20141013-1735(启用S​​SL)iOS上的IBM AppCenter 6.2.0 + SSL - 连接服务器时 ...

  3. VC2010打开资源视图时提示“指南必须指定类型”,.rc资源文件损坏(转)

    VC: 打开资源视图时提示"指南必须指定类型 "(Guideline--specify type) ,.rc资源文件损坏 可能是TFS导致的 使用记事本打开.rc文件,找到&quo ...

  4. vue监听路由的变化,跳转到同一个页面时,Url改变但视图未重新加载问题

    vue监听路由的变化,跳转到同一个页面时,Url改变但视图未重新加载问题 解决方法: 添加路由监听,路由改变时执行监听方法 methods:{fetchData(){console.log('路由发送 ...

  5. mysql中创建视图语法错误_mysql - 使用过程MySQL创建视图时出错 - 堆栈内存溢出

    尝试使用过程创建视图时出现问题. 我必须这样做,因为我需要在MySQL中发挥作用,将表的行转换为另一个表的列. 该查询效果很好,但是当我将其放在" CREATE VIEW"语句中时 ...

  6. VS Code加载 Web 视图时出错

    问题描述: VS Code加载 Web 视图时出错: Error: Could not register service workers: InvalidStateError: Failed to r ...

  7. oracle查询视图时慢,oracle视图查询速度慢

    一般对视图的查询最好都分解为对数据表的 直接查询效果要好一些. 可以在参数文件中设置SHARED_POOL_RESERVED_SIZE参 数,这个参数在SGA共享池中保留一个连续的内存空间,连 续的内 ...

  8. html.partial传递模型,关于使用Html.RenderPartial和Html.Partial显示分部视图时提示参

    今天在使用使用Html.RenderPartial和Html.Partial显示分部视图时遇到了一个颇为奇怪的问题: 视图页面A: @model DataResult @Html.Partial(&q ...

  9. ORACLE 创建视图时,出现权限不足 ORA-01031:权限不足

    在使用CREATE VIEW view_orcl AS ... 来创建视图时,如果使用的是Oracle 10g或者之前的版本,则使用scott用户登录后,时可以直接创建视图的:但如果用户使用的是Ora ...

最新文章

  1. 雷军这个程序员!真的牛逼!
  2. ROS之用程序设定导航目标点
  3. 斯坦福大学公开课:iPhone开发教程2010年冬
  4. .net 裁剪图片(不压缩)
  5. ubuntu中显示本机的gpu_Ubuntu下实时查看Nvidia显卡显存占用情况和GPU温度
  6. 电脑密码忘记怎么办??
  7. 压缩的问题-----WriteUp
  8. HDU 3328 Flipper 栈 模拟
  9. 开万人 AI 大会什么感受?陆奇任大会主席 | AI ProCon 2020
  10. FAQ系列 | mysqldump选项之skip-opt
  11. Dart基础第3篇:常用数据类型、字符串类型、数值类型、布尔类型、List集合类型、Maps类型
  12. 《Java 多线程编程核心技术》- 笔记
  13. html中怎么让照片变模糊,CSS改变图片由模糊到清晰
  14. bzoj4200: [Noi2015]小园丁与老司机(可行流+dp)
  15. MyBioSource STMN1 elisa 试剂盒解决方案
  16. 如何在AI(Adobe illustrator)里用角标
  17. SWUSTOJ #954 单链表的链接
  18. SDU信息门户(8)组队和文件系统分析
  19. bms中soh计算方式_BMS算法设计之电池SOH介绍(上)
  20. Debian 安装sublime 安装拼音输入法 安装谷歌浏览器

热门文章

  1. 如何以子类的形式运行多进程?
  2. 软件评测-信息安全-应用安全-资源控制-用户登录限制(上)
  3. CentOS 6.3 samba安装及配置
  4. 中兴3G模块 MU301
  5. Dynamips使用说明
  6. 对.Net 垃圾回收的C#编程相关方面(Finalize 和Dispose(bool disposing)和 Dispose())的一些理解体会...
  7. 详解JavaScript数组过滤相同元素的5种方法
  8. css选择器参考手册
  9. Android 最火高速开发框架AndroidAnnotations使用具体解释
  10. EF Code First建库 增删改查