更改视图宽度约束

时间:2019-03-20 13:26:49

标签: swift autolayout

我这样设置UIView的宽度:

subscribe(onNext:

现在我要更改宽度。问题是:如何删除旧约束并添加新约束?我确实这样创建了contentView.widthAnchor.constraint(equalToConstant: CGFloat(scrollWidth())).isActive = true

NSLayoutConstraint

如何为scrollWidthConstraint = NSLayoutConstraint( item: contentView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: CGFloat(scrollWidth()) ) 设置该约束以便以后更改?

1 个答案:

答案 0 :(得分:3)

首先更好地使用它,并简短地为其指定一个引用

var widCon:NSLayoutConstraint! 

widCon = contentView.widthAnchor.constraint(equalToConstant: CGFloat(scrollWidth()))
widCon.isActive = true

代替NSLayoutConstraint(item: contentView,,然后将其更改为常量

widCon.constant = ////