iOS以编程方式删除约束问题

iOS以编程方式删除约束问题,ios,constraints,xib,nslayoutconstraint,Ios,Constraints,Xib,Nslayoutconstraint,我有一个用于TableViewCell的xib,在这个单元格中,我有4个标签堆叠在彼此的顶部。想象一下: Label1 Label2 Label3 Label4 我在每个标签的底部添加了一个约束,使它们之间有3个垂直空间。现在,在xib文件中执行此操作时,它还会在3的每个其他标签的顶部“创建”一个约束。所以基本上是这样的: Label1我创建了一个底部约束,所以它也在Label2上创建了一个顶部约束,但它们指向同一个“对象”。等等,等等 问题是,在运行时,根据数据的不同,我不想显示其中一些标签

我有一个用于TableViewCell的xib,在这个单元格中,我有4个标签堆叠在彼此的顶部。想象一下:

Label1
Label2
Label3
Label4
我在每个标签的底部添加了一个约束,使它们之间有3个垂直空间。现在,在xib文件中执行此操作时,它还会在3的每个其他标签的顶部“创建”一个约束。所以基本上是这样的:

Label1我创建了一个底部约束,所以它也在Label2上创建了一个顶部约束,但它们指向同一个“对象”。等等,等等

问题是,在运行时,根据数据的不同,我不想显示其中一些标签。例如,我可能只需要3个标签,因此在这种情况下,我希望显示Label1、Label2和Label3。但我也不希望Label4占据任何空间,因为我不希望我的手机比它必须的大

所以我做了这样的事情:

我将label3的底部约束连接到我的视图控制器中,并将其称为label3BottomConstraint,然后执行(以及更多操作,但这是最重要的部分):

我这样做了,但我在日志中看到这样的消息:

  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']>"
在屏幕/模拟器上,它看起来与我想要的完全一样,但我在日志中看到了许多这样的消息:

        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)]
无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。试着这样做:(1)看看每个约束,试着找出你不期望的约束;(2) 找到添加了不需要的约束的代码,然后修复它。(注意:如果您看到不理解的NSAutoresizingMaskLayoutConstraints,请参阅UIView属性TranslatesAutoResizingMaskToConstraints的文档)
(
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
NSLayoutConstraint:0x7fedd974a7a0 V:[UIImageView:0x7fedd974fd90(42)]

在这种情况下,我所做的任何事情似乎都没有受到重视。

您能提供更多关于您设置的约束的详细信息吗?从您描述的方式来看,因为您正在执行
label1.bottom-label2.top
间距约束,所以
label3BottomConstraint
应该引用
label4.top
。所以从你所描述的,所有应该发生的事情,就是你正在移除3磅的垂直空间,但是标签仍然挂着around@Killian我添加了一个显示更多细节的编辑。
    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)]