NSLayoutConstraints打破,但似乎罚款
问题描述:
我试着对图像视图顶部,左侧,和上海华权,并与NSLayoutConstraint
给它的高度,像这样:NSLayoutConstraints打破,但似乎罚款
backgroundView.addSubview(imageView)
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Top, relatedBy: .Equal, toItem: backgroundView, attribute: .Top, multiplier: 1.0, constant: 0.0))
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Left, relatedBy: .Equal, toItem: backgroundView, attribute: .Left, multiplier: 1.0, constant: 0.0))
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Right, relatedBy: .Equal, toItem: backgroundView, attribute: .Right, multiplier: 1.0, constant: 0.0))
backgroundView.addConstraint(NSLayoutConstraint(item: imageView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: view.bounds.width))
但图像没有按不出现,调试器记录我的约束被破坏了。我看不出有什么限制可能导致问题。
答
通过在将imageView作为子视图添加到backgroundView之前添加imageView.translatesAutoresizingMaskIntoConstraints = false
来修复。