我已经做了一个小示范来展示我的应用程序正在发生什么。基本上,我想为左上角的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