我正在更新旧的应用程序,AdBannerView当没有广告时,它会从屏幕上滑落.当有广告时,它会在屏幕上滑动.基本的东西.
旧样式,我在动画块中设置框架.新风格,我有一个IBOutlet确定Y位置的约束,在这种情况下,它与superview底部的距离,并修改常量.
- (void)moveBannerOffScreen {
[UIView animateWithDuration:5 animations:^{
_addBannerDistanceFromBottomConstraint.constant = -32;
}];
bannerIsVisible = FALSE;
}
- (void)moveBannerOnScreen {
[UIView animateWithDuration:5 animations:^{
_addBannerDistanceFromBottomConstraint.constant = 0;
}];
bannerIsVisible = TRUE;
}
Run Code Online (Sandbox Code Playgroud)
横幅移动,完全符合预期,但没有动画.
更新:我重新观看了WWDC12视频" 掌握自动布局的最佳实践 ",其中包括动画.它讨论了如何使用更新约束AdBannerView.

我尝试使用以下代码,但得到完全相同的结果.
- (void)moveBannerOffScreen {
_addBannerDistanceFromBottomConstraint.constant = -32;
[UIView animateWithDuration:2 animations:^{
[self.view setNeedsLayout];
}];
bannerIsVisible = FALSE;
}
- (void)moveBannerOnScreen {
_addBannerDistanceFromBottomConstraint.constant = 0;
[UIView animateWithDuration:2 animations:^{
[self.view setNeedsLayout];
}];
bannerIsVisible = TRUE;
}
Run Code Online (Sandbox Code Playgroud)
在旁注中,我已经多次检查过,这是在主线程上执行的.
我知道汽车布局链基本上包含3个不同的过程.
我不完全清楚的是-setNeedsLayout和之间的内在差异-setNeedsUpdateConstraints.来自Apple Docs:
如果要调整视图子视图的布局,请在应用程序的主线程上调用此方法.此方法记录请求并立即返回.由于此方法不强制立即更新,而是等待下一个更新周期,因此可以在更新任何视图之前使用它来使多个视图的布局无效.此行为允许您将所有布局更新合并到一个更新周期,这通常会提高性能.
当自定义视图的属性以会影响约束的方式更改时,可以调用此方法以指示需要在将来的某个时刻更新约束.然后系统将调用updateConstraints作为其正常布局传递的一部分.在需要之前立即更新约束可确保在布局过程之间对视图进行多次更改时,不会不必要地重新计算约束.
当我想在修改约束后为视图设置动画并为我通常调用的更改设置动画:
[UIView animateWithDuration:1.0f delay:0.0f usingSpringWithDamping:0.5f initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
[self.modifConstrView setNeedsUpdateConstraints];
[self.modifConstrView layoutIfNeeded];
} completion:NULL];
Run Code Online (Sandbox Code Playgroud)
我发现,如果我使用-setNeedsLayout的,而不是-setNeedsUpdateConstraints一切都按预期工作,但如果我改变-layoutIfNeeded用-updateConstraintsIfNeeded,动画将不会发生.
我试图得出自己的结论:
-updateConstraintsIfNeeded 仅更新约束但不强制布局进入该过程,因此仍保留原始帧-setNeedsLayout也叫-updateContraints方法那么什么时候可以使用一个而不是另一个?关于布局方法,我是否需要在约束或父视图中更改的视图上调用它们?
我的问题很简单,但我在文档中找不到任何信息.
从视图层次结构中删除视图(或移动到另一个视图)时,布局约束会发生什么?
例如,让我们容器C与子视图A和B.容器C有一些约束.然后我们打电话[A removeFromSuperview].约束会发生什么A?
那么如果我们增加会发生什么A来C着?
在下面的代码中,调用这四种方法进行布局推理.我有点困惑为什么所有这些都是需要的,以及它们彼此不同的做法.它们在过程中用于通过自动布局使单元格的高度变为动态.(来自该库从这个问题.)
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
[cell.contentView setNeedsLayout];
[cell.contentView layoutIfNeeded];
Run Code Online (Sandbox Code Playgroud)
它来自这个单元格高度的代码块:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
RJTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell updateFonts];
NSDictionary *dataSourceItem = [self.model.dataSource objectAtIndex:indexPath.row];
cell.titleLabel.text = [dataSourceItem valueForKey:@"title"];
cell.bodyLabel.text = [dataSourceItem valueForKey:@"body"];
cell.bodyLabel.preferredMaxLayoutWidth = tableView.bounds.size.width - (kLabelHorizontalInsets * 2.0f);
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
[cell.contentView setNeedsLayout];
[cell.contentView layoutIfNeeded];
CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
return height;
}
Run Code Online (Sandbox Code Playgroud)
但他们的做法有何不同?他们为什么都需要?