ios 带有自动布局约束的 UIView 动画变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21014087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIView animated changes with auto layout constraints
提问by Skoota
Say that I have a project which looks like the following:
假设我有一个如下所示的项目:
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
.
有两个UIView
s - 一个称为yellowBox
,另一个称为redBox
。自动布局约束规定yellowBox
距离父视图顶部 60 点,前导和尾随空格(即视图的左侧和右侧)为 350 点。在redBox
具有相同的开头和结尾的空间限制。两个 0 框之间存在垂直空间约束,以指示 的底部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:
当用户点击 Expand Yellow Box 按钮时,我希望 的高度yellowBox
增加,因此,redBox
向下移动以继续满足垂直空间约束(yellowBox
始终在 顶部redBox
)。下面是动画代码:
- (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:
但是,我一直无法使其正常工作。正如您通过红色图标所看到的,目前约束存在问题:
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:
这很公平,因为自动布局无法知道框的高度。但是,我不知道如何以允许调整和移动框的方式解决此问题。例如,如果我在 上指定高度约束yellowBox
,则将阻止其调整大小。如果我将高度约束设置为大于或等于(以允许yellowBox
高度增加),那么我会得到一个不同的约束错误:
All constraints have been established using Xcode in the storyboard - none have been written in code.
所有的约束都是在故事板中使用 Xcode 建立的——没有一个是用代码编写的。
Any help greatly appreciated.
非常感谢任何帮助。
EDIT: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
.
编辑:事实证明,yellowBox
单击按钮时 正在增长 - 只是我无法分辨,因为它出现在redBox
. 我只在点击它四次后才注意到它开始出现在redBox
. 然而,同样的问题仍然存在——如何让redBox
始终坚持在yellowBox
.
回答by GoGreen
Try it as mentioned by shwet-solanki, but add the following line after changing the constraint:
按照shwet-solanki所述尝试,但在更改约束后添加以下行:
[self.view layoutIfNeeded];
the IBAction would look like:
IBAction 看起来像:
- (IBAction)expandYellowBox:(id)sender {
[UIView animateWithDuration:0.5
animations:^{
self.yellowBoxHeightConstraint.constant += 50;
[self.view layoutIfNeeded];
}];
}
Where yellowBoxHeightConstraint
is the IBOutlet
for height constraint of yellowBox
.
Hope this helps.
yellowBoxHeightConstraint
的IBOutlet
高度约束在哪里yellowBox
。希望这可以帮助。
回答by Shwet Solanki
- Add Height constraint to both the views
- Create an IBOutlet for height constraint of yellowbox
- Now instead of changing the height of yellowbox in the button pressed event, rather change the value of the height constraint. i.e suppose your IBOutlet constraint name is yellowBoxHeightConstraint, then
yellowBoxHeightConstraint.constant += 50;
- 向两个视图添加高度约束
- 为 Yellowbox 的高度约束创建一个 IBOutlet
- 现在不是在按钮按下事件中更改黄色框的高度,而是更改高度约束的值。即假设您的 IBOutlet 约束名称是 YellowBoxHeightConstraint,然后
yellowBoxHeightConstraint.constant += 50;
Hope this works for you.
希望这对你有用。
回答by CoolMonster
//
// WPViewController.h
// AutoLayoutTEst
//
// Created by VASANTH K on 09/01/14.
//
//
#import <UIKit/UIKit.h>
@interface WPViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *button1;
- (IBAction)buttClicked:(id)sender;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstrain;
@property (strong, nonatomic) IBOutlet UIButton *button2;
@end
click Event
点击事件
- (IBAction)buttClicked:(id)sender {
self.heightConstrain.constant=100;
}
here u need to set the heightConstrain
for the Yellow button then create reference outlet for that button then update the heightConstrain
to update the size of that button it will automatically move your Red button down.
在这里,您需要heightConstrain
为黄色按钮设置 ,然后为该按钮创建参考插座,然后更新heightConstrain
以更新该按钮的大小,它将自动向下移动您的红色按钮。
https://github.com/vasanth3008/AutoLayoutHeighDemo
https://github.com/vasanth3008/AutoLayoutHeighDemo
refer my sample demo project
参考我的示例演示项目