简体   繁体   中英

NSLayoutConstraint.constraintsWithVisualFormat pass nil in options parameter in Swift 2.0

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.

尝试:

self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[categoryTitleLabel]|", options: [], metrics: nil, views: views))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM