12

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

iPera
  • 1,053
  • 1
  • 12
  • 24
  • Have you tried using plain `Dictionary` instead of `NSMutableDictionary`? Like `let viewBindingsDict = ["webView" : self.webView!]` ? – Michal Jul 27 '15 at 08:41
  • Also, I believe `addConstraints` may not like the fact, that the `webView` in the `NSMutableDictionary` is optional. – Michal Jul 27 '15 at 08:43
  • Michal I'm tried with Dictionary with same result. Look at screenshot, Xcode is pointed at "H:|[webView]|" as problem. – iPera Jul 27 '15 at 08:54

1 Answers1

23

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))
Dániel Nagy
  • 11,815
  • 9
  • 50
  • 58