tajihiro
tajihiro

Reputation: 2443

How to use function NSLayoutConstraint.constraintsWithVisualFormat on Swift?

I'm trying Swift and using layout format. The IDE I'm using is Xcode6-Beta2.

    var viewDictionary:Dictionary = ["myButton": myButton]

    var constraintFormat1:AnyObject[]! = 
            NSLayoutConstraint.constraintsWithVisualFormat("H:|-[myButton]-|",
        options : NSLayoutFormatOptions(0),
         metrics: nil,
           views: viewDictionary)

But I got error message below.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: 'Unable to parse constraint format: 
Unable to interpret '|' character, because the related view doesn't have a superview 
H:|-[myButton] 
              ^'

Doesn't it work on swift? Or I write wrong format pattern?

Upvotes: 0

Views: 4523

Answers (2)

Ash
Ash

Reputation: 5712

I had same problem, I checked in details , I was not adding subview to my superview , so it was complaining for

" Unable to interpret '|' character, because the related view doesn't have a superview 
H:|-[yourSubView]"

Make sure you called addSubview(youSubview) too.

Upvotes: 0

Wolf McNally
Wolf McNally

Reputation: 3657

The "|" (vertical bar) character refers to the superview of the view(s) you're declaring as inside of it (directly or indirectly) in this case "myButton". When you call .constraintsWithVisualFormat, it tries to implicitly find the superview of your button so it can create constraints between that view and your button. However, as your button has not yet been added to a view, it fails.

Upvotes: 8

Related Questions