BlackDeveraux BlackDeveraux - 4 months ago 19
iOS Question

Swift Error: Unresolved Identifier "NSLayoutAttributeWidth"

I need to apply some constraints programmatically in Swift to a UIView using this code but I'm receiving an error of "Unresolved Identifier" for NSLayoutAttributeWidth and NSLayoutRelationEqual.

The error appears on line 7 below:

func setViews(){
addSubview(aGradeView)
addSubview(bGradeView)

addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-280-[viewA(==viewB)]-0-[viewB]-0-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["viewA": aGradeView, "viewB": bGradeView]))

let constraint: NSLayoutConstraint = NSLayoutConstraint(item: aGradeView, attribute: NSLayoutAttributeWidth, relatedBy: NSLayoutRelationEqual, toItem: bGradeView, attribute: NSLayoutAttributeWidth, multiplier: 1.0, constant: 5.0)

/*addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[viewA]-0-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["viewA": aGradeView]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[viewB]-0-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["viewB": bGradeView]))
*/
}

let aGradeView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(red: 207.0/255.0, green: 240.0/255.0, blue: 158.0/255.0, alpha: 255.0)
view.translatesAutoresizingMaskIntoConstraints = false
view.setconstr
return view
}()

let bGradeView: UIView = {
let view = UIView()
view.backgroundColor = UIColor(red: 150.0/255.0, green: 240.0/255.0, blue: 158.0/255.0, alpha: 255.0)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()


I've imported UIKit, and attempted to clean the project a few times, but no avail. How can I resolve this issue?

Thanks in advance!
BD

Answer

In swift, you can just use .Width instead NSLayoutAttributeWidth or use NSLayoutAttribute.Width

So your constraint should be like this

 let constraint = NSLayoutConstraint(item: aGradeView, attribute: .Width, relatedBy: .Equal, toItem: bGradeView, attribute: .Width, multiplier: 1.0, constant: 5.0)