[英]How to change bottom layout constraint in iOS, Swift
I have scroll view as @IBOutlet 我有滚动视图@IBOutlet
@IBOutlet weak var mainScrollView: UIScrollView!
I want to change the 我想改变
"Bottom space to: Bottom Layout Guide"
constraint programmatically. 约束编程。
First Item : Bottom Layout Guide.Top
Relation : Equal
Second Item: Scroll View.Bottom
Constant: 0 -> 50 // (I want to change this programmatically)
Priority: 1000
Multiplier: 1
How can I do this? 我怎样才能做到这一点?
Take the constraint as IBOutlet
of NSLayoutConstraint
. 将约束作为
NSLayoutConstraint
IBOutlet
。
Set the constraint outlets and change constant
value by : 设置约束出口并通过以下方式更改
constant
量值:
self.sampleConstraint.constant = 20
self.view.layoutIfNeeded()
If you are adding constraint programatically like this: 如果您以编程方式添加约束,请执行以下操作:
var constraintButton = NSLayoutConstraint (item: buttonPlay,
attribute: NSLayoutAttribute.Bottom,
relatedBy: NSLayoutRelation.Equal,
toItem: self.view,
attribute: NSLayoutAttribute.Bottom,
multiplier: 1,
constant: 0)
// Add the constraint to the view
self.view.addConstraint(constraintButton)
Then you can update it this way: 然后你可以这样更新它:
self.constraintButton.constant = 50
self.view.layoutIfNeeded()
And if you want that with animation you can do it this way: 如果您想要动画,可以这样做:
self.view.layoutIfNeeded()
UIView.animateWithDuration(1, animations: {
self.constraintButton.constant = 50
self.view.layoutIfNeeded()
})
Hope it helps. 希望能帮助到你。
Create an IBOutlet
for your constraint: 为您的约束创建一个
IBOutlet
:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomContraint;
And when you need to change it call: 当你需要改变它时,请致电:
bottomContstraint.constant = //your value
view.layoutIfNeeded()
Also you can animate constraint change like that: 您也可以像这样设置约束更改:
bottomContstraint.constant = //your value
UIView.animateWithDuration(0.5, animations: {
self.view.layoutIfNeeded()
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.