简体   繁体   English

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:未知的布局属性'

[英]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. 你有两个项目: arrowButtonself ,但你指定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.

相关问题 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'UICollectionView必须使用非nil布局参数初始化 - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“数据参数为nil” - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[SWRevealViewController manifestToggel:] - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SWRevealViewController revealToggel:] 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[FBSDKProfilePictureView CGImage]: - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FBSDKProfilePictureView CGImage]: 由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“-[MKUserLocation标记]: - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKUserLocation tag]: 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [UIButton setText:] - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setText:] 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFString isDescendantOfView:]: - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString isDescendantOfView:]: 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:NSArrayM objectForKey - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: NSArrayM objectForKey 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFBoolean length] - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean length] 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [Home isEqualToString:] - Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Home isEqualToString:]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM