So I have a project, and UI in the project is completely coded (no .xib files or storyboards at all).
With Objective-C, we could pass nil
or 0
as an argument for options
parameter. Like this
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|-[one(two)]-[two(three)]-[three]-|" options: nil metrics:metrics views:views]];
Swift 1.2 also allowed us to pass nil as an argument
self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[categoryTitleLabel]|", options: nil, metrics: nil, views: views))
But in latest Xcode 7 beta 6 with Swift 2.0 options parameter now requires non-optional value, and I'm a little bit stuck.
I assumed, that earlier, if we passed nil
in options
parameter, then some default value from NSLayoutFormatOptions
is used. I looked at NSLayoutFormatOptions
declaration and found that DirectionLeadingToTrailing
is the default value. So I replaced all nil arguments as .DirectionLeadingToTrailing
. It worked, but the app started to crash with
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: A constraint cannot be made between a leading/trailing attribute and a right/left attribute. Use leading/trailing for both or neither
I have realised that some constraints are conflicting, but the fact is that apparently, .DirectionLeadingToTrailing
is not the default value. If not, then what is? Current workaround is passing NSLayoutFormatOptions(rawValue: 0)
as an argument but it looks ugly.