自 xcode 6.3 以来 NSLayoutconstraint 损坏
NSLayoutconstraint breakage since xcode 6.3
我正在尝试将以下约束设置为我的浮动视图之一。
leftConstraint = [NSLayoutConstraint constraintWithItem:detailView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:VIEW_WIDTH];
视图将水平移动并具有固定宽度。我无法将此视图固定到任何其他视图。我将更改约束中的常量值以将其移动到我的视图上。
当我 运行 上述 XCode 6.3 中的约束时,出现以下错误。
"A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant"
我不确定为什么会产生非法约束。
为了绕过这个问题,我使用了 0.001 乘数,但这并不是在所有情况下都有效。因此,正在寻找更好的解决方法来满足此要求。
这是自动版式使用的公式:
item1.attribute1 = multiplier × item2.attribute2 + constant
因此,如果您将乘数设置为 0 或没有商品 2,则属性 1 将为 constant
。这不是一个好主意,因为它是关于 superview 的约束而不提及 superview。在大多数情况下,这是代码错误的结果。所以 Apple 决定创建这种限制是非法的。
没有第二项的约束对除高度和宽度之外的所有属性均无效。
您实际上应该针对视图的父视图显式添加约束。
您的约束应如下所示
leftConstraint = [NSLayoutConstraint constraintWithItem:detailView
attribute: NSLayoutAttributeLeft
relatedBy: NSLayoutRelationEqual
toItem: detailView.superview
attribute: NSLayoutAttributeLeft
multiplier: 1
constant: VIEW_WIDTH];
如果您想将视图放置在左侧并添加宽度约束,则必须使用两个约束:
CGFloat leftConstraintConstant = 40.0f;
CGFloat VIEW_WIDTH = 200.0f;
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:detailView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:leftConstraintConstant];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:detailView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:VIEW_WIDTH];
[self.view addSubview:detailView];
[self.view addConstraints:@[leftConstraint,widthConstraint]];
要正确定位您的视图,您还应该为 Y 轴位置和高度添加单独的约束。
请注意,我尚未测试此代码,但它应该能让您简要了解需要做什么。
我正在尝试将以下约束设置为我的浮动视图之一。
leftConstraint = [NSLayoutConstraint constraintWithItem:detailView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0
constant:VIEW_WIDTH];
视图将水平移动并具有固定宽度。我无法将此视图固定到任何其他视图。我将更改约束中的常量值以将其移动到我的视图上。
当我 运行 上述 XCode 6.3 中的约束时,出现以下错误。 "A multiplier of 0 or a nil second item together with a location for the first attribute creates an illegal constraint of a location equal to a constant"
我不确定为什么会产生非法约束。
为了绕过这个问题,我使用了 0.001 乘数,但这并不是在所有情况下都有效。因此,正在寻找更好的解决方法来满足此要求。
这是自动版式使用的公式:
item1.attribute1 = multiplier × item2.attribute2 + constant
因此,如果您将乘数设置为 0 或没有商品 2,则属性 1 将为 constant
。这不是一个好主意,因为它是关于 superview 的约束而不提及 superview。在大多数情况下,这是代码错误的结果。所以 Apple 决定创建这种限制是非法的。
没有第二项的约束对除高度和宽度之外的所有属性均无效。
您实际上应该针对视图的父视图显式添加约束。
您的约束应如下所示
leftConstraint = [NSLayoutConstraint constraintWithItem:detailView
attribute: NSLayoutAttributeLeft
relatedBy: NSLayoutRelationEqual
toItem: detailView.superview
attribute: NSLayoutAttributeLeft
multiplier: 1
constant: VIEW_WIDTH];
如果您想将视图放置在左侧并添加宽度约束,则必须使用两个约束:
CGFloat leftConstraintConstant = 40.0f;
CGFloat VIEW_WIDTH = 200.0f;
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:detailView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:leftConstraintConstant];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:detailView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:VIEW_WIDTH];
[self.view addSubview:detailView];
[self.view addConstraints:@[leftConstraint,widthConstraint]];
要正确定位您的视图,您还应该为 Y 轴位置和高度添加单独的约束。 请注意,我尚未测试此代码,但它应该能让您简要了解需要做什么。