Using Autolayout Visual Format with Swift?

后端 未结 9 1980
执笔经年
执笔经年 2021-02-01 14:57

I\'ve been trying to use the Autolayout Visual Format Language in Swift, using NSLayoutConstraint.constraintsWithVisualFormat. Here\'s an example of some code that

9条回答
  •  攒了一身酷
    2021-02-01 15:44

    The first gotcha here is that Swift Dictionary is not yet bridged with NSDictionary. To get this to work, you'll want to explicitly create a NSDictionary for each NSDictionary-typed parameters.

    Also, as Spencer Hall points out, {} isn't a dictionary literal in Swift. The empty dictionary is written:

    [:]
    

    As of XCode 6 Beta 2, this solution allows you to create constraints with the visual format:

    var viewBindingsDict: NSMutableDictionary = NSMutableDictionary()
    viewBindingsDict.setValue(fooView, forKey: "fooView")
    viewBindingsDict.setValue(barView, forKey: "barView")
    self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[fooView]-[barView]-|", options: nil, metrics: nil, views: viewBindingsDict))
    

提交回复
热议问题