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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:02:55  来源:igfitidea点击:

UIView animated changes with auto layout constraints

ioscocoa-touchuiviewautolayout

提问by Skoota

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

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

enter image description here

在此处输入图片说明

There are two UIViews - one called yellowBoxand the other called redBox. The auto layout constraints dictate that the yellowBoxis 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 redBoxhas 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 yellowBoxshould always be directly on top of the redBox.

有两个UIViews - 一个称为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 yellowBoxto increase and, as a result, the redBoxmoves down to continue satisfying the vertical space constraint (that the yellowBoxis 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:

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

enter image description here

在此处输入图片说明

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 yellowBoxthen that will prevent it from being resized. If I set the height constraint to be greater than or equal(to allow the yellowBoxheight to increase) then I get a different constraint error:

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

enter image description here

在此处输入图片说明

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 yellowBoxis 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 redBoxto 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 yellowBoxHeightConstraintis the IBOutletfor height constraint of yellowBox. Hope this helps.

yellowBoxHeightConstraintIBOutlet高度约束在哪里yellowBox。希望这可以帮助。

回答by Shwet Solanki

  1. Add Height constraint to both the views
  2. Create an IBOutlet for height constraint of yellowbox
  3. 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;
  1. 向两个视图添加高度约束
  2. 为 Yellowbox 的高度约束创建一个 IBOutlet
  3. 现在不是在按钮按下事件中更改黄色框的高度,而是更改高度约束的值。即假设您的 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 heightConstrainfor the Yellow button then create reference outlet for that button then update the heightConstrainto 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

参考我的示例演示项目