Swift 2.0 constraintsWithVisualFormat

时间:2015-07-27 08:37:07

标签: swift autolayout swift2

我在Swift 2.0中的AutoLayout出现了奇怪的问题

self.webView = WKWebView()
self.view.addSubview(self.webView!)

var viewBindingsDict: NSMutableDictionary = NSMutableDictionary()
viewBindingsDict.setValue(self.webView, forKey: "webView")

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: 0, metrics: nil, views: viewBindingsDict))

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[webView]|", options: 0, metrics: nil, views: viewBindingsDict))

我收到错误:

'String' is not convertible to 'StringLiteralConvertible'

“H:| [webView] |”“V:| [webView] |”

我不知道我是否遗漏了某些东西

更新: Screenshot from playground

1 个答案:

答案 0 :(得分:23)

您应该将viewBindigsDict更改为

var viewBindingsDict = [String: AnyObject]()
viewBindingsDict["webView"] = webView

根据评论中的建议,以及格式选项,您不能使用0,而是:

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewBindingsDict))

或更简单,如@MartinR建议:

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: [], metrics: nil, views: viewBindingsDict))