隐藏使用自动版式约束一个UIView [英] Hiding a UIView using AutoLayout constraints

查看:234
本文介绍了隐藏使用自动版式约束一个UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我有我想从布局中删除一个子视图。不仅它应该被隐藏,但它不应该被认为是视图的流的一部分,可以这么说。举个例子:

From time to time I have a subview that I would like to remove from a layout. Not only should it be hidden, but it should not be considered part of the view's 'flow', so to speak. An example:

我要寻找到隐藏编程橙色视图的战略。的盒,并且其内容的布局,是通过自动布局。有两点需要注意:

I am looking for a strategy to hide the orange view programmatically. The layout of the boxes, and their content, is via autolayout. Two things to note:


  • 橙色框定义其垂直高度根据内容,加上一些边缘顶部/底部的偏移量。因此,标签设置文本零只会缩水的观点到它的内部空间,就不会为0的高度。

  • 类似地,三个框之间的垂直间距意味着即使橙色框的高度为0,红色和黄色之间的间隙的要求将是两倍大。

我最好的建议是一个约束添加到橙色框,设置它的高度为0。对于这个工作,我需要使用非优先需要所有的橙色框内部的垂直约束。同时,容器应该更新该分隔框约束的常数。我不喜欢这种方法,因为橙色框类与其说是定义它的内部制约与它的考虑上海华的行为。也许我可以忍受它,如果橙色盒子视图,而不是暴露了一个崩溃的方法,增加了0高度约束自己。

My best suggestion is to add a constraint to the orange box, setting it's height to 0. For this to work, I need to use non-required priorities for all of the vertical constraints inside the orange box. At the same time, the container should update the constant for the constraint that separates the boxes. I don't like this approach so much since the orange box class is defining it's internal constraints with it's superview's behavior in mind. Perhaps I could live with it if the orange box view instead exposes a 'collapse' method that adds the 0 height constraint itself.

有没有更好的方法吗?

推荐答案

您可以通过添加额外的约束优先级较低的黄色和红色的观点之间,并且调整code中的优先做到这一点。

You can do this by adding an extra constraint between the yellow and red views of a lower priority, and adjusting the priorities in code.

短虚线约束(orangeToRedCon是出口)有999优先级(你不能要求一个优先级改变为一个非必需的,所以这就是为什么它不是1000)。长虚线约束(yellowToRedCon)具有500优先级和20恒定在code,可以隐藏橙色观点和交换这些优先级,这将导致黄以向上移动到任何值你已经设置yellowToRedCon的恒定值。

The short dashed constraint (orangeToRedCon is the outlet) has a priority of 999 (you can't change a required priority to a non-required, so that's why it's not 1000). The long dashed constraint (yellowToRedCon) has a priority of 500 and a constant of 20. In code, you can hide the orange view, and swap those priority levels, and that will cause the yellow view to move up to whatever value you've set for the constant value of yellowToRedCon.

-(void)changePriorities {
    self.yellowToRedCon.priority = 999;
    self.orangeToRedCon.priority = 500;
    [UIView animateWithDuration:.5 animations:^{
        self.orangeView.alpha = 0;
        [self.view layoutIfNeeded];
    }];
}

此方法不需要在橙色视图的高度上的任何变化。

This method doesn't require any changes in the orange view's height.

这篇关于隐藏使用自动版式约束一个UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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