Swift错误:未解析的标识符" NSLayoutAttributeWidth"

时间:2016-07-23 06:25:28

标签: ios swift xcode nslayoutconstraint

我需要使用此代码在Swift中以编程方式将一些约束应用于UIView,但我收到错误

  

NSLayoutAttributeWidth和NSLayoutRelationEqual的未解析标识符。

错误显示在下面第7行:

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
}()

我已导入UIKit,并试图清理该项目几次,但无济于事。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

在swift中,您可以使用.Width代替NSLayoutAttributeWidth或使用NSLayoutAttribute.Width

所以你的约束应该是这样的

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