[英]Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: Unknown layout attribute'
Im trying to add a constraint programmatically to a button inside a UIView
: 我试图以编程方式将约束添加到
UIView
内的按钮:
let constraint = NSLayoutConstraint(item: arrowButton, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal,
toItem: self, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 10)
arrowButton.addConstraint(constraint)
I want the leading margin of the button to be 10 from the left margin of the view. 我希望按钮的前边距距离视图的左边距为10。 This code is run inside the view so
self
is the view. 此代码在视图内部运行,因此
self
是视图。 The code above is giving the error: 上面的代码给出了错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Unknown layout attribute'
What am I doing wrong? 我究竟做错了什么?
NSLayoutAttributeNotAnAttribute: NSLayoutAttributeNotAnAttribute:
A placeholder value that is used to indicate that the constraint's second item and second attribute are not used in any calculations. 占位符值,用于指示约束的第二个项目和第二个属性未在任何计算中使用。 Use this value when creating a constraint that assigns a constant to an attribute.
在创建为属性指定常量的约束时使用此值。 For example, item1.height >= 40. If a constraint only has one item, set the second item to nil, and set the second attribute to NSLayoutAttributeNotAnAttribute.
例如,item1.height> = 40. 如果约束只有一个项目,则将第二个项目设置为nil,并将第二个属性设置为NSLayoutAttributeNotAnAttribute。
You are having two items: arrowButton
and self
, but you specify NSLayoutAttributeNotAnAttribute
saying I just need one item. 你有两个项目:
arrowButton
和self
,但你指定NSLayoutAttributeNotAnAttribute
说我只需要一个项目。 That's why it is failed. 这就是它失败的原因。
I suggest you use storybard or xib to practice the auto layout first by feeling how it works, and then start coding it. 我建议您先使用storybard或xib来练习自动布局,然后再感觉它是如何工作的,然后开始编码。 You clearly misunderstand auto layout.
你明显误解了自动布局。 If you are defining two view's layout relation, then
NSLayoutAttributeNotAnAttribute
will never be valid to use. 如果要定义两个视图的布局关系,则
NSLayoutAttributeNotAnAttribute
将永远无效。
NSLayoutAttribute.NotAnAttribute
, NSLayoutAttribute.NotAnAttribute
,
I think here is the problem. 我想这就是问题所在。 You pass in
NotAnAttribute
您传入
NotAnAttribute
Also,it should be self.addConstraint(constraint)
此外,它应该是
self.addConstraint(constraint)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.