iOS 设置动画视图中的自动输出符合所有设备
iOS set the autolyout in the animation view comply with all the device
我有一个关于 iOS 中的自动布局的问题。
我研究了好几个星期,但我仍然没有达到我需要的行动。 / \
我正在构建一个动画视图(容器视图)。容器视图中有一个点击按钮。
初始化状态如下图所示:
单击按钮,容器视图将使用动画(滑动)到底部按钮上部。
并且容器会覆盖白色部分(底部按钮上部)。
最后的愿望如下:
然后单击容器视图中的按钮。它会将视图折叠回初始状态。
我可以编写代码来控制容器视图的约束。
- (IBAction)slideBtnAction:(id)sender {
if (isContainerOpen) {
// 100 is for user click region
// it is slide view action (close)
[self replaceTopConstraintOnView:self.containerView withConstant: -self.containerView.frame.size.height + 40];
} else {
// it is slide view action(open)
[self replaceTopConstraintOnView:self.containerView withConstant: self.containerView.frame.size.height - _btmBtn1.frame.size.height - 40];
}
[self animateConstraints];
isContainerOpen = !isContainerOpen;
}
- (void)replaceTopConstraintOnView:(UIView *)view withConstant:(float)constant
{
[self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if ((constraint.firstItem == view) && (constraint.firstAttribute == NSLayoutAttributeTop)) {
constraint.constant = constant;
}
}];
}
- (void)animateConstraints
{
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
}
但是站台是container view fixed height 动画无法滑动到bottom upper position的正确位置(遮住白色部分)
我希望容器视图能够兼容所有iOS设备。所以容器视图将动态高度。
但我不知道如何在幻灯片站中设置容器视图高度。(我在这些代码中实现的修复高度。)
有谁能帮我实现一下效果吗?
因为我在情节提要中设置了一些自动布局。
所以我将我的简单项目添加到 github[here]。
请指点和指导,非常感谢..
--------------------编辑------------
@YuviGr
如果我更改约束(使用您的建议)
// it is slide view action(open)
[self replaceTopConstraintOnView:self.containerView
withConstant: self.containerView.bounds.size.height - _btmBtn1.bounds.size.height];
- (void)replaceTopConstraintOnView:(UIView *)view withConstant:(float)constant
{
[self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if ((constraint.firstItem == view) && (constraint.firstAttribute == NSLayoutAttributeTop)) {
constraint.constant = constant;
}
}];
}
当我单击按钮时,视图没有动画到正确的位置。
像这些
首先,您需要创建对要设置动画的约束的引用,在这种情况下,我认为是 containerView 的底部约束。
只需按住 control 从 Storyboard 拖动到控制器即可创建 IBOutlat(如果您不知道该怎么做,请查看本教程Link)
要达到 containerView 可见的状态(第二张图片),您可以计算视图的高度并减去按钮的高度
constraint = self.view.bounds.size.height - button.bounds.size.height;
对于不可见的(第一张图片),您必须使用常数来确定您想要的高度
我有一个关于 iOS 中的自动布局的问题。
我研究了好几个星期,但我仍然没有达到我需要的行动。 / \
我正在构建一个动画视图(容器视图)。容器视图中有一个点击按钮。
初始化状态如下图所示:
单击按钮,容器视图将使用动画(滑动)到底部按钮上部。
并且容器会覆盖白色部分(底部按钮上部)。
最后的愿望如下:
然后单击容器视图中的按钮。它会将视图折叠回初始状态。
我可以编写代码来控制容器视图的约束。
- (IBAction)slideBtnAction:(id)sender {
if (isContainerOpen) {
// 100 is for user click region
// it is slide view action (close)
[self replaceTopConstraintOnView:self.containerView withConstant: -self.containerView.frame.size.height + 40];
} else {
// it is slide view action(open)
[self replaceTopConstraintOnView:self.containerView withConstant: self.containerView.frame.size.height - _btmBtn1.frame.size.height - 40];
}
[self animateConstraints];
isContainerOpen = !isContainerOpen;
}
- (void)replaceTopConstraintOnView:(UIView *)view withConstant:(float)constant
{
[self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if ((constraint.firstItem == view) && (constraint.firstAttribute == NSLayoutAttributeTop)) {
constraint.constant = constant;
}
}];
}
- (void)animateConstraints
{
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded];
}];
}
但是站台是container view fixed height 动画无法滑动到bottom upper position的正确位置(遮住白色部分)
我希望容器视图能够兼容所有iOS设备。所以容器视图将动态高度。
但我不知道如何在幻灯片站中设置容器视图高度。(我在这些代码中实现的修复高度。)
有谁能帮我实现一下效果吗?
因为我在情节提要中设置了一些自动布局。
所以我将我的简单项目添加到 github[here]。
请指点和指导,非常感谢..
--------------------编辑------------
@YuviGr
如果我更改约束(使用您的建议)
// it is slide view action(open)
[self replaceTopConstraintOnView:self.containerView
withConstant: self.containerView.bounds.size.height - _btmBtn1.bounds.size.height];
- (void)replaceTopConstraintOnView:(UIView *)view withConstant:(float)constant
{
[self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if ((constraint.firstItem == view) && (constraint.firstAttribute == NSLayoutAttributeTop)) {
constraint.constant = constant;
}
}];
}
当我单击按钮时,视图没有动画到正确的位置。
像这些
首先,您需要创建对要设置动画的约束的引用,在这种情况下,我认为是 containerView 的底部约束。
只需按住 control 从 Storyboard 拖动到控制器即可创建 IBOutlat(如果您不知道该怎么做,请查看本教程Link)
要达到 containerView 可见的状态(第二张图片),您可以计算视图的高度并减去按钮的高度
constraint = self.view.bounds.size.height - button.bounds.size.height;
对于不可见的(第一张图片),您必须使用常数来确定您想要的高度