我已經做了一個小示範來展示我的應用程序正在發生什麼。基本上,我想爲左上角的UIView設置動畫,佔用整個屏幕寬度的一半,佔據整個寬度,並在屏幕右上角「推」UIView。然後在稍後的動畫中,我想將UIView放回到屏幕上,並將左側的視圖重新調整爲其開始的半屏寬度。用Autolayout改變UIView的寬度
我已經採取了一些截圖來證明與我現在有它的方式會發生什麼。 這是怎麼開始的: 然後調整左視圖 試圖將其帶回 這裏經過後是改變意見的大小不同的代碼
- (void)replaceWidthConstraintOnView:(UIView *)view withConstant:(float)constant {
[self.view.constraints enumerateObjectsUsingBlock:^(NSLayoutConstraint *constraint, NSUInteger idx, BOOL *stop) {
if ((constraint.firstItem == view) && (constraint.firstAttribute == NSLayoutAttributeWidth)) {
constraint.constant = constant;
}
}];
}
- (IBAction)changeViewSize {
CGFloat blueViewWidth = self.blueView.bounds.size.width;
if (blueViewWidth == self.view.bounds.size.width) {
blueViewWidth = blueViewWidth/2;
[self replaceWidthConstraintOnView:self.blueView withConstant:blueViewWidth];
}
else {
blueViewWidth = blueViewWidth * 2;
[self replaceWidthConstraintOnView:self.blueView withConstant:blueViewWidth];
}
}
我想知道爲什麼藍色視圖不會覆蓋屏幕的一半。我感謝任何幫助!
你可以嘗試以下方法:通過constraint.constant =常數設置新的寬度後,插入[containerView layoutIfNeeded]; containerView是你的藍圖的超級視圖。 – 2014-10-04 19:45:53