我在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] |”
我不知道我是否遗漏了某些东西
答案 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))