Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 由于未捕获异常而终止应用程序';NSInvalidArgumentException';,原因:未知布局属性';_Ios_Swift - Fatal编程技术网

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

Ios 由于未捕获异常而终止应用程序';NSInvalidArgumentException';,原因:未知布局属性';,ios,swift,Ios,Swift,我正试图以编程方式将约束添加到ui视图中的按钮中: let constraint = NSLayoutConstraint(item: arrowButton, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1

我正试图以编程方式将约束添加到
ui视图中的按钮中

let constraint = NSLayoutConstraint(item: arrowButton, attribute: NSLayoutAttribute.LeadingMargin, relatedBy: NSLayoutRelation.Equal, 
                 toItem: self, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 10)
arrowButton.addConstraint(constraint)
我希望按钮的前缘距视图的左边缘为10。此代码在视图中运行,因此
self
是视图。上面的代码给出了错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: Unknown layout attribute'

我做错了什么?

nslayouttribute.NotAnAttribute

我想这就是问题所在。您传入
NotAnAttribute


此外,它应该是self.addConstraint(constraint)

nsLayoutAttribute属性:

占位符值,用于指示约束的第二项和第二个属性未在任何计算中使用。创建为属性指定常量的约束时使用此值。例如,item1.height>=40如果约束只有一个项,则将第二个项设置为nil,并将第二个属性设置为nsLayoutAttribute属性。

您有两个项目:
arrowButton
self
,但您指定的
nslayoutatributenatAttribute
表示我只需要一个项目。这就是它失败的原因


我建议您使用storybard或xib,首先通过感觉自动布局的工作原理来练习自动布局,然后开始编写代码。您显然误解了自动布局。如果要定义两个视图的布局关系,则
nslayouttributenotataAttribute
将永远无法有效使用。

5分钟后我将使用,它将允许我:)