Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2.0 constraintsWithVisualFormat

I have strange problem with AutoLayout in Swift 2.0

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

And I'm receiving error:

'String' is not convertible to 'StringLiteralConvertible'

for "H:|[webView]|" and "V:|[webView]|"

I do not know if im missing something

Update: Screenshot from playground

like image 375
iPera Avatar asked Jul 27 '15 08:07

iPera


1 Answers

You should change your viewBindigsDict to

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

as suggested in the comments, and also in the format options, you cannot use 0, instead:

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

or more simplier as @MartinR suggested:

view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: [], metrics: nil, views: viewBindingsDict))
like image 60
Dániel Nagy Avatar answered Nov 17 '22 08:11

Dániel Nagy