如何激活約束更改?我正在用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;}橫幅的移動和預期完全一樣,但是不動畫。最新情況:我重新看了WWDC 12談掌握汽車布局的最佳實踐包括動畫。討論如何使用共動畫: 我嘗試了以下代碼,但得到了完全相同的結果:- (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;}另外,我檢查了很多次,這是在主線。
3 回答

躍然一笑
TA貢獻1826條經驗 獲得超6個贊
你需要打電話 layoutIfNeeded
在動畫塊中。實際上,Apple建議您在動畫塊之前調用它一次,以確保所有掛起的布局操作都已完成。 您需要在 父視圖(如: self.view
),而不是附加約束的子視圖。這樣做將更新 全約束視圖,包括動畫其他可能被限制為您更改了視圖A的約束的視圖(例如,視圖B附加到視圖A的底部,而您剛剛更改了視圖A的頂部偏移量,您希望視圖B與它一起動畫)
目標-C
- (void)moveBannerOffScreen { [self.view layoutIfNeeded]; [UIView animateWithDuration:5 animations:^{ self._addBannerDistanceFromBottomConstraint.constant = -32; [self.view layoutIfNeeded]; // Called on parent view }]; bannerIsVisible = FALSE;}- (void)moveBannerOnScreen { [self.view layoutIfNeeded]; [UIView animateWithDuration:5 animations:^{ self._addBannerDistanceFromBottomConstraint.constant = 0; [self.view layoutIfNeeded]; // Called on parent view }]; bannerIsVisible = TRUE;}
SWIFT 3
UIView.animate(withDuration: 5) { self._addBannerDistanceFromBottomConstraint.constant = 0 self.view.layoutIfNeeded()}

慕哥9229398
TA貢獻1877條經驗 獲得超6個贊
文檔中的基本塊動畫
[containerView layoutIfNeeded]; // Ensures that all pending layout operations have been completed[UIView animateWithDuration:1.0 animations:^{ // Make all constraint changes here [containerView layoutIfNeeded]; // Forces the layout of the subtree animation block and then captures all of the frame changes}];
updateConstraints
調用子視圖updateConstraint方法的動畫塊
[self.view layoutIfNeeded];[self.subView setNeedsUpdateConstraints];[self.subView updateConstraintsIfNeeded]; [UIView animateWithDuration:1.0f delay:0.0f options:UIViewAnimationOptionLayoutSubviews animations:^{ [self.view layoutIfNeeded];} completion:nil];
- (void)updateConstraints{ // Update some constraints [super updateConstraints];}
UISwitch
UITextField
- 3 回答
- 0 關注
- 648 瀏覽
添加回答
舉報
0/150
提交
取消