我有一个用于TableViewCell的xib,在这个单元格中,我有4个标签堆叠在一起。想象一下:
Label1
Label2
Label3
Label4
我在每个标签的底部添加了一个约束,使它们之间有3个垂直空间。现在,当您在xib文件中执行此操作时,它还会在每个其他3个标签的顶部“创建”约束。所以基本上就是这样:
Label1我创建了一个底部约束,因此它还在Label2上创建了一个顶部约束,但它们指向相同的“对象”。等等,等等......
问题是,在运行时,根据数据,我不想显示其中的一些标签。例如,我可能只想要3个标签,所以在这种情况下我想要显示Label1,Label2和Label3。但我也不希望Label4占用任何空间,因为我不希望我的Cell变得比它更大。
所以我这样做:
我将label3的底部约束连接到我的视图控制器并将其称为label3BottomConstraint,然后执行(以及更多,但这是重要部分):
label3BottomConstraint.constant = 0;
我这样做并且有效,但我在日志中看到这样的消息:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
"<NSLayoutConstraint:0x7f913c1d0fc0 V:[UILabel:0x7f913e84d850'30%']-(3)-[UILabel:0x7f913e84e240'90% < 18']>"
因此即使我将底部约束设置为0,xib文件创建的label4的顶部约束仍然存在,即使它们实际上是相同的NSLayoutConstraint对象。那么如何删除这个?我是否应该担心这些消息?
编辑:我试图通过删除标签来简化它,以便我可以更容易地描述它:
所以在中间我有一个UIView围绕两个图像水平居中,mP和fP标签,以及mLabel和fLabel。这个UIView叫做dataContainer。
dataContainer有顶部空间8,底部空间8,这些是通过属性dataContainerBottomSpaceConstraint和dataContainerTopSpaceConstraint连接的。
两个图像都有高度42和底部2.这些图像通过属性{m / f} ImageHeightConstraint和{m / f} ImageBottomConstraint连接。
mP和fP都有高度17和底部2,并且缺少更好的单词“继承”图像中的前2位。它们通过属性{mP / fP} HeightConstraint和{mP / fP} BottomConstraint连接。
mLabel1和fLabel1都具有高度15,底部3和“继承”mP / fP底部2的前2位。这些属性通过{m / f} Line1BottomConstraint和{m / f} Line1HeightConstraint连接。
在运行时,我现在的目标是删除整个dataContainer,以便在单元格上显示最底层的东西是“x num”。我不能只删除这个子视图,因为它可以显示数据。所以我做了以下几点:
cell.dataContainerBottomSpaceConstraint.constant = 0;
cell.dataContainerTopSpaceConstraint.constant = 8;
cell.dataContainer.hidden = YES;
cell.mImageHeightConstraint.constant = 0;
cell.fImageHeightConstraint.constant = 0;
cell.mImageBottomConstraint.constant = 0;
cell.fImageBottomConstraint.constant = 0;
cell.mImage.hidden = YES;
cell.flmage.hidden = YES;
cell.mPBottomConstraint.constant = 0;
cell.fPBottomConstraint.constant = 0;
cell.mPHeightConstraint.constant = 0;
cell.fPLabelHeightConstraint.constant = 0;
cell.mPLabel.hidden = YES;
cell.fPLabel.hidden = YES;
cell.mLine1BottomConstraint.constant = 0;
cell.fLine1BottomConstraint.constant = 0;
cell.mLine1HeightConstraint.constant = 0;
cell.fLine1HeightConstraint.constant = 0;
cell.mLine1.hidden = YES;
cell.fLine1.hidden = YES;
在屏幕/模拟器上,它看起来与我想要的完全一样,但我在日志中看到了很多这样的消息:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7feddb8b3540 V:[UIImageView:0x7feddb8b3420(0)]>",
"<NSLayoutConstraint:0x7fedd974a7a0 V:[UIImageView:0x7fedd974fd90(42)]>",
"<NSLayoutConstraint:0x7feddb9b2210 V:[UILabel:0x7feddb938360'mP'(17)]>",
"<NSLayoutConstraint:0x7feddb941250 V:[UILabel:0x7feddb9e81f0'mLabel1'(15)]>",
"<NSLayoutConstraint:0x7feddb954e50 V:[UILabel:0x7feddb94a180'fLabel1'(15)]>",
"<NSLayoutConstraint:0x7feddb91fff0 V:[UILabel:0x7feddb943cb0'fP'(17)]>",
"<NSLayoutConstraint:0x7feddb94cb10 V:|-(0)-[UIImageView:0x7feddb8b3420] (Names: '|':UIView:0x7fedd9696640 )>",
"<NSLayoutConstraint:0x7feddb949900 V:[UIImageView:0x7feddb8b3420]-(2)-[UILabel:0x7feddb938360'mP']>",
"<NSLayoutConstraint:0x7feddb949b20 V:[UILabel:0x7feddb938360'mP']-(2)-[UILabel:0x7feddb9e81f0'mLabel1']>",
"<NSLayoutConstraint:0x7feddb949b70 V:[UILabel:0x7feddb9e81f0'mLabel1']-(3)-| (Names: '|':UIView:0x7fedd9696640 )>",
"<NSLayoutConstraint:0x7feddb949c80 V:|-(0)-[UIImageView:0x7fedd974fd90] (Names: '|':UIView:0x7fedd9696640 )>",
"<NSLayoutConstraint:0x7feddb949ea0 V:[UIImageView:0x7fedd974fd90]-(2)-[UILabel:0x7feddb943cb0'fP']>",
"<NSLayoutConstraint:0x7feddb949f60 V:[UILabel:0x7feddb94a180'fLabel1']-(3)-| (Names: '|':UIView:0x7fedd9696640 )>",
"<NSLayoutConstraint:0x7feddb949fb0 V:[UILabel:0x7feddb943cb0'fP']-(2)-[UILabel:0x7feddb94a180'fLabel1']>"
)
Will attempt to recover by breaking constraint
NSLayoutConstraint:0x7fedd974a7a0 V:[UIImageView:0x7fedd974fd90(42)]
在这种情况下,它甚至看起来不像我正在做的任何事情都被接受了。