移动视图与约束
我在视图控制器中有几个视图,当检测到向上滑动时向上移动,然后在检测到向下滑动时向下移动。 我通过使用CGRectOffset调整y原点来迫使视图移动。 现在,我已经对IB的视图应用了约束,我不确定最佳的方式来移动视图,以便他们在iPhone 5,6和6 +的最终位置。
目前我正在做这样的事情:
[self.view layoutIfNeeded]; self.panFrameVerticalConstraint.constant = self.panFrameVerticalConstraint.constant +338; [UIView animateWithDuration:5 animations:^{ [self.view layoutIfNeeded]; }];
用比例来改变常量是否更好? 所以对于上面的约束,而不是使用338,最好是这样做:
self.panFrameVerticalConstraint.constant = self.panFrameVerticalConstraint.constant + (self.panView.frame.size.height/1.680); //self.panView.frame.size.height = 568 //(568/1.680) = 338
是的,当你改变常量没有问题。 这里的事情是你必须适当地设置你的约束的事情。
我们来看一个例子。
我有一个故事板UIView
,我想改变它的宽度。 它的宽度是1024
。
经过一些animation,我想把它的宽度改为900
。
以下几点我必须遵循以实现这一点:
- select我们想要更新约束的
UIView
。 这里我们需要更新width
,所以我们将为宽度添加约束。
- 现在我们可以看到添加到
UIView
新约束。
- 点击那个约束。 它将显示约束属性。 这里现在我们要把宽度从1024减小到900,这样在约束属性中把Relation属性改为Less than或者Equal 。 同样,如果你需要增加默认的宽度,那么Relation将会大于或等于 。
- 现在为
NSLayoutConstraint
创build一个IBOutlet
variables,并将其与上面的宽度约束连接起来。
variables看起来像:
目标C代码:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *viewWidthConstraint;
SWIFT代码:
@IBOutlet weak var viewWidthConstraint : NSLayoutConstraint!
- 代码来改变我们需要的常量:
为了减less我们使用下面的代码:
目标C代码:
// Reduce width of view. [UIView animateWithDuration:0.35f animations:^{ self.viewWidthConstraint.constant = 900; [self.view layoutIfNeeded]; }];
SWIFT代码:
// Reduce width of view. UIView.animateWithDuration(0.35, animations: { () -> Void in self.viewWidthConstraint.constant = 900 self.view.layoutIfNeeded() })
同样我们可以将其更改为默认值:
目标C代码:
// Change to default width of view. [UIView animateWithDuration:0.35f animations:^{ self.viewWidthConstraint.constant = 1024; [self.view layoutIfNeeded]; }];
SWIFT代码:
// Change to default width of view. UIView.animateWithDuration(0.35, animations: { () -> Void in self.viewWidthConstraint.constant = 1024 self.view.layoutIfNeeded() })
- 无法更改UIInputView高度
- 无法同时满足约束embedded在故事板中的AVPlayerViewController的警告
- AutoLayout和UILabel调整animation在设备旋转时无法正常工作
- 在Swift 3中以编程方式使用ScrollView
- 当我将UIPanGestureRecognizer和自动布局结合在一起时,我的UIViews变得糟糕透顶
- UICollectionViewCell systemLayoutSizeFittingSize返回不正确的宽度
- 使用UIStackViewdynamicUITableView行高度?
- UICollectionView iOS 9在支持RTL语言的项目上发布
- 问题与iOS 8自动布局(代码在iOS 7完美的作品)