自动布局约束的UIView动画的变化
说,我有一个项目,如下所示:
Say that I have a project which looks like the following:
有两个的UIView
秒 - 一个叫 yellowBox
另一种叫 REDBOX
。自动布局约束决定了在 yellowBox
是从350点前后的空格(即到左视图右)上海华顶60分。在 REDBOX
具有相同的前端和后端空间的限制。有0两箱之间的垂直空间约束来指示的底部 yellowBox
总是应该是直接的 REDBOX 顶code>。
There are two UIView
s - 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
.
当用户点击展开黄盒子按钮,我想 yellowBox
来提高,作为一个结果是,红盒子的高度
向下移动,继续满足垂直空间的限制(即在 yellowBox
总是在 REDBOX $ C顶部$ C>)。这里是动画code:
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
那么就会prevent它高度约束。如果我设置高度限制为大于或等于的(允许 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:
所有的限制已经用X code。在故事板建立的 - 都没有被写在code
All constraints have been established using Xcode in the storyboard - none have been written in code.
任何帮助非常AP preciated。
Any help greatly appreciated.
编辑:事实证明,在 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-索兰奇,但改变后的约束添加以下行:
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];
}];
}
其中, yellowBoxHeightConstraint
是 IBOutlet中
为高度约束 yellowBox
。
希望这可以帮助。
Where yellowBoxHeightConstraint
is the IBOutlet
for height constraint of yellowBox
.
Hope this helps.