简体   繁体   English

NSLayoutConstraint.constraintsWithVisualFormat在Swift 2.0中的options参数中传递nil

[英]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). 因此,我有一个项目,并且该项目中的UI已完全编码(根本没有.xib文件或情节提要)。 With Objective-C, we could pass nil or 0 as an argument for options parameter. 使用Objective-C,我们可以传递nil0作为options参数的参数。 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 Swift 1.2还允许我们将nil作为参数传递

  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. 但是,在最新的带有Swift 2.0选项参数的Xcode 7 beta 6中,现在需要非可选值,我有点卡住了。 I assumed, that earlier, if we passed nil in options parameter, then some default value from NSLayoutFormatOptions is used. 我之前假设,如果我们在options参数中传递nil ,那么将使用NSLayoutFormatOptions一些默认值。 I looked at NSLayoutFormatOptions declaration and found that DirectionLeadingToTrailing is the default value. 我查看了NSLayoutFormatOptions声明,发现DirectionLeadingToTrailing是默认值。 So I replaced all nil arguments as .DirectionLeadingToTrailing . 因此,我将所有nil参数替换为.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. 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'*** + [NSLayoutConstraintconstraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]:无法在前导/跟踪属性与right /之间建立约束左属性。 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. 我已经意识到一些约束是冲突的,但事实是,显然.DirectionLeadingToTrailing不是默认值。 If not, then what is? 如果没有,那是什么? Current workaround is passing NSLayoutFormatOptions(rawValue: 0) as an argument but it looks ugly. 当前的解决方法是将NSLayoutFormatOptions(rawValue: 0)作为参数传递,但是看起来很丑。

尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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