Ios 当放置在固定大小的UIView下时,使用autolayout调整UIView的大小
我将自动布局用于此布局: 带边框的自视图(0,80320488) 带框架的collectionView(0,0320220) 带边框的欠视图(0220320268) 并尝试实现一个动画,该动画应调整underView的大小,并扩大其高度,以使underView部分覆盖自底向上移动的collectionView Autolayout为这两个视图强制执行高度约束,因此我为underView高度约束创建了一个IBOutlet,我正在动画中对其进行调整:Ios 当放置在固定大小的UIView下时,使用autolayout调整UIView的大小,ios,autolayout,nslayoutconstraint,Ios,Autolayout,Nslayoutconstraint,我将自动布局用于此布局: 带边框的自视图(0,80320488) 带框架的collectionView(0,0320220) 带边框的欠视图(0220320268) 并尝试实现一个动画,该动画应调整underView的大小,并扩大其高度,以使underView部分覆盖自底向上移动的collectionView Autolayout为这两个视图强制执行高度约束,因此我为underView高度约束创建了一个IBOutlet,我正在动画中对其进行调整: NSLog(@"underView heigh
NSLog(@"underView height %.2f", self.underView.frame.size.height);
NSLog(@"offset (%.2f, %.2f)", offset.x, offset.y);
heightConstraint.constant += offset.y;
NSLog(@"heightConstraint %.2f", heightConstraint.constant);
[self.underView setNeedsUpdateConstraints];
[UIView animateWithDuration:ANIMATION_DURATION
delay:ANIMATION_DELAY
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.underView layoutIfNeeded];
NSLog(@"underView height %.2f", heightConstraint.constant);
} completion:nil];
在控制台中,我可以看到:
2013-07-30 09:09:37.268 Cal[49611:a0b] underView height 268.00
2013-07-30 09:09:37.268 Cal[49611:a0b] offset (320.00, 144.00)
2013-07-30 09:09:37.269 Cal[49611:a0b] heightConstraint 412.00
2013-07-30 09:09:37.270 Cal[49611:a0b] underView height 412.00
但调整大小根本不会发生
我能想到的这种行为的唯一原因是上面collectionView的高度约束,但由于我不想重新加载集合,因此无法对其进行操作
我想知道如何在collectionView上实现欠视放大?我尝试使用约束优先级,但它不起作用…根据尝试以下操作。确保在superview上调用layoutifneed
:
[UIView animateWithDuration:ANIMATION_DURATION
delay:ANIMATION_DELAY
options:UIViewAnimationOptionCurveEaseOut
animations:^{
heightConstraint.constant += offset.y;
[self.view layoutIfNeeded];
}
completion:nil];
因此,我找到了一个可能的解决方案,但不确定这是否是最好的解决方案。我缺少对self.collectionView的垂直空间约束的引用,因此最终代码是:
verticalSpace.constant -= shift;
heightConstraint.constant += shift;
[self.collectionView setNeedsUpdateConstraints];
[self.underView setNeedsUpdateConstraints];
[UIView animateWithDuration:ANIMATION_DURATION
delay:ANIMATION_DELAY
options:UIViewAnimationOptionCurveEaseOut
animations:^{
[self.view layoutIfNeeded];
} completion:nil
];
我将等待其他更好的解决方案一段时间,这样我就不会将自己的答案标记为已被接受的答案。谢谢@Maarten,但实际上这没有任何区别,而且,如果你阅读了对答案的评论,你链接的人已经确认,持续更新可以很好地避开动画,在动画块中仅保留layoutIfNeeded方法。作为一个额外的提示,我还尝试调用[self.view layoutifneed],正如有人指出的,这是需要重新显示布局的superview,但没有再次更改。您是否尝试调用superview上的
layoutifneed
?我改变了我的答案以反映这一点。是的,正如我上面所说,self.view是超级视图。但是在你的代码中,你将layoutifneed
称为undowview
,而不是self.view
,就像我在回答中所做的那样。@Marteen,在我上面的第一句评论中:“作为一个额外的提示,我也尝试调用了[self.view layoutifneed]正如有人指出的,superview需要重新显示布局,但没有任何改变”。