带有自动布局约束的 UIView 动画变化 [英] UIView animated changes with auto layout constraints

查看:22
本文介绍了带有自动布局约束的 UIView 动画变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个如下所示的项目:

Say that I have a project which looks like the following:

有两个UIView——一个叫做yellowBox,另一个叫做redBox.自动布局约束规定 yellowBox 距离父视图顶部 60 点,前导和尾随空格(即视图的左侧和右侧)为 350 点.redBox 具有相同的前导和尾随空格约束.两个 0 框之间有一个垂直空间约束,表示 yellowBox 的底部应始终直接位于 redBox 的顶部.

There are two UIViews - one called yellowBox and the other called redBox. The auto layout constraints dictate that the yellowBox is 60 points from the top of the superview with 350 points leading and trailing spaces (i.e. to left and right of the view). The redBox has the same leading and trailing space constraints. There is a vertical space constraint between the two boxes of 0 to indicate that the bottom of the yellowBox should always be directly on top of the redBox.

当用户点击 Expand Yellow Box 按钮时,我希望 yellowBox 的高度增加,结果 redBox 向下移动以继续满足垂直空间约束(yellowBox 总是在 redBox 之上).这是动画代码:

When the user taps the Expand Yellow Box button, I would like the height of the yellowBox to increase and, as a result, the redBox moves down to continue satisfying the vertical space constraint (that the yellowBox is always on top of the redBox). Here is the animation code:

- (IBAction)expandYellowBox:(id)sender {

    [UIView animateWithDuration:0.5
                     animations:^{
                         CGRect newFrame = self.yellowBox.frame;
                         newFrame.size.height += 50;
                         self.yellowBox.frame = newFrame;
                     }];

}

但是,我一直无法使其正常工作.正如您通过红色图标所看到的,此时约束存在问题:

However, I have been unable to get this working properly. As you can see by the red icon, there is a problem with the constraints at the moment:

这很公平,因为自动布局无法知道框的高度.但是,我不知道如何以允许调整大小和移动框的方式解决此问题.例如,如果我在 yellowBox 上指定一个高度约束,那么这将阻止它被调整大小.如果我将高度约束设置为大于或等于(以允许 yellowBox 高度增加),那么我会得到一个不同的约束错误:

which is fair enough, as there's no way for auto layout to know the height of the boxes. However, I don't know how to resolve this issue in such a way that it will allow for the boxes to be resized and moved. For example, if I specify a height constraint on the yellowBox then that will prevent it from being resized. If I set the height constraint to be greater than or equal (to allow the yellowBox height to increase) then I get a different constraint error:

所有的约束都是在故事板中使用 Xcode 建立的——没有一个是用代码编写的.

All constraints have been established using Xcode in the storyboard - none have been written in code.

非常感谢任何帮助.

事实证明,当单击按钮时 yellowBox 正在增长 - 只是我无法分辨,因为它出现在 redBox 后面.我只在点击它四次后才注意到它开始出现在 redBox 的底部.然而,同样的问题仍然存在——如何让 redBox 始终贴在 yellowBox 的底部.

As it turns out, the yellowBox is growing when the button is clicked - it's just I couldn't tell because it appears behind the redBox. I only noticed after clicking it around four times and it started appearing out the bottom of the redBox. However, the same question still remains - how to get the redBox to always stick to the bottom of the yellowBox.

推荐答案

按照 shwet-solanki,但在更改约束后添加以下行:

Try it as mentioned by shwet-solanki, but add the following line after changing the constraint:

[self.view layoutIfNeeded];

IBAction 看起来像:

the IBAction would look like:

- (IBAction)expandYellowBox:(id)sender {

[UIView animateWithDuration:0.5
                 animations:^{
                     self.yellowBoxHeightConstraint.constant += 50;
                     [self.view layoutIfNeeded];
                 }];
}

其中yellowBoxHeightConstraintyellowBox 高度约束的IBOutlet.希望这可以帮助.

Where yellowBoxHeightConstraint is the IBOutlet for height constraint of yellowBox. Hope this helps.

这篇关于带有自动布局约束的 UIView 动画变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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