下面的代码我有多个视图划分所有视图相等的宽度,并且还成功地划分所有视图的宽度相等,但不会根据前一个视图划分空间增加。请解决此问题.customView是我的超级视图。
for i in 0..<countNoOfViews{
var customView1:UIView!
customView1 = UIView(frame: CGRect.zero)
customView1.translatesAutoresizingMaskIntoConstraints = false
customView.addSubview(customView1)
let widthConstraint1 = NSLayoutConstraint(item: customView1, attribute: .width, relatedBy: .equal,toItem: customView, attribute: .width, multiplier: 1/CGFloat(countNoOfViews), constant: 0)
let leadingConstraint = NSLayoutConstraint(item: customView1, attribute: .leading, relatedBy: .equal, toItem: customView, attribute: .leading, multiplier: 1.0, constant: 0)
let topConstraint = NSLayoutConstraint(item: customView1, attribute: .top, relatedBy: .equal,toItem: customView, attribute: .top, multiplier: 1.0, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: customView1, attribute: .bottom, relatedBy: .equal,toItem: customView, attribute: .bottom, multiplier: 1.0, constant: 0)
let trailingConstraint2 = NSLayoutConstraint(item: customView1, attribute: .trailing, relatedBy: .equal, toItem: customView, attribute: .trailing, multiplier:1.0, constant: 0)
if i == 0{
customView1.backgroundColor = UIColor.black
customView.addConstraints([topConstraint,bottomConstraint,leadingConstraint,widthConstraint1])
}else if i == 1{
customView1.backgroundColor = UIColor.red
customView.addConstraints([leadingConstraint,topConstraint,bottomConstraint,widthConstraint1])
}else if i == 2{
customView1.backgroundColor = UIColor.purple
customView.addConstraints([leadingConstraint,topConstraint,bottomConstraint,trailingConstraint2])
}
}