NSLayoutConstraints错误

时间:2014-12-23 03:27:31

标签: ios swift nslayoutconstraint programmatically-created

我喜欢在代码中布局约束..但由于某种原因,我最新的swift项目正在以我的方式抛出错误..我的约束代码就是这样。

    let views = ["optionsView":optionsView, "cameraView":cameraView, "imageView":imageView]
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[optionsView(300)]-[cameraView(300)]-[imageView(300)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[optionsView(500)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[cameraView(500)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[imageView(500)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))

对于所有四个约束,Xcode抛出一个错误,告诉我最后一个函数参数 - views - 无效。因此错误消息是:

'String' is not identical to 'NSObject'

任何想法? PLZ,谢谢你

1 个答案:

答案 0 :(得分:1)

问题是NSLayoutFormatOptions(0)。只需说nil

哦,你的所有观点都是Optionals。你需要解开所有这些!

let views = ["optionsView":optionsView!, "cameraView":cameraView!, "imageView":imageView!]