当以编程方式实现自动布局时,iOS获取NSInvalidArgumentException
我通过编程实现了自动布局当以编程方式实现自动布局时,iOS获取NSInvalidArgumentException,ios,xcode,autolayout,Ios,Xcode,Autolayout,我通过编程实现了自动布局 self.rightSideLine = [[UIView alloc] init]; self.rightSideLine.backgroundColor = COLOR_MojorColor_Depper; [self.rightSideLine setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.view addSubview:self.rightSideLine]; [self addConstrain
self.rightSideLine = [[UIView alloc] init];
self.rightSideLine.backgroundColor = COLOR_MojorColor_Depper;
[self.rightSideLine setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.rightSideLine];
[self addConstraintToRightSideLine];
添加约束方法:
- (void)addConstraintToRightSideLine
{
NSLayoutConstraint *constraintToAnimate1 = [NSLayoutConstraint constraintWithItem:self.rightSideLine
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:0.00
constant:0.0];
[self.view addConstraint:constraintToAnimate1];
NSLayoutConstraint *constraintToAnimate2 = [NSLayoutConstraint constraintWithItem:self.rightSideLine
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.00
constant:-IPAD];
[self.view addConstraint:constraintToAnimate2];
NSLayoutConstraint *constraintToAnimate3 = [NSLayoutConstraint constraintWithItem:self.rightSideLine
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:0.00
constant:IPAD];
[self.view addConstraint:constraintToAnimate3];
NSLayoutConstraint *constraintToAnimate4 = [NSLayoutConstraint constraintWithItem:self.rightSideLine
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:1.00
constant:0.0];
[self.view addConstraint:constraintToAnimate4];
}
这段代码在设备上运行得非常好,但当我运行ion simulator时,我得到了NSInvalidArgumentException:
原因:'***+[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:乘数:常量::::'***+[NSLayoutConstraint constraintWithItem:attribute:multiplier:constant:::]:乘数为0或零秒的项与第一个属性的位置一起,会对等于常量的位置创建非法约束。位置属性必须成对指定
您的乘数不能为0.0。它必须大于0,但为什么它在设备上工作?我想设置X=0。@也许你的设备运行的是不同版本的iOS。这个限制来自于iOS的更高版本(不记得确切是哪个版本),为什么要将乘数设置为0?你想实现什么?我有几个视图是用自动布局编程创建的。我使用自动布局创建了所有这些。self.rightSideLine是ViewController中最上面的视图,因此我将X=0,Y=0。