约束导致异常

时间:2016-03-26 19:47:03

标签: ios uitableview autolayout constraints tableviewcell

我在initWithStyle:reuseIdentifier:子类的UITableViewCell方法中创建了这些约束:

[self.visualEffectView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[visualEffectView(50)]|" options:0 metrics:nil views:@{@"visualEffectView" : self.visualEffectView}]];

这应该约束UIVisualEffectView占据屏幕的整个宽度,同时高50点并坚持在单元格的底部。

但是,它显示正确,但会导致此异常:

无法同时满足约束条件。

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x15c7783b0 UIVisualEffectView:0x15c5ae220.top == CustomCell:0x15c5ac140'cell'.topMargin>

我在这里做错了什么?

更新

setup

调用

initWithStyle:reuseIdentifier:

-(void)setup
{
    self.profileImageView = [UIImageView new];
    self.profileImageView.contentMode = UIViewContentModeScaleToFill;
    self.visualEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];

    [self addSubview:self.profileImageView];
    [self addSubview:self.visualEffectView];

    [self.profileImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.visualEffectView setTranslatesAutoresizingMaskIntoConstraints:NO];

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[profileImageView]|" options:0 metrics:nil views:@{@"profileImageView" : self.profileImageView}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[profileImageView]|" options:0 metrics:nil views:@{@"profileImageView" : self.profileImageView}]];

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[visualEffectView]|" options:0 metrics:nil views:@{@"visualEffectView" : self.visualEffectView}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[visualEffectView(50)]|" options:0 metrics:nil views:@{@"visualEffectView" : self.visualEffectView}]];

}

完整的错误消息:

"(
"<NSLayoutConstraint:0x12c5b2c40 UIVisualEffectView:0x12c753b90.top == CustomCell:0x12c751bb0'cell'.topMargin>",
"<NSLayoutConstraint:0x12c5b2ec0 V:[UIVisualEffectView:0x12c753b90(50)]>",
"<NSLayoutConstraint:0x12c5b2f10 V:[UIVisualEffectView:0x12c753b90]-(0)-|   (Names: '|':CustomCell:0x12c751bb0'cell' )>",
"<NSLayoutConstraint:0x12c5ba110 'UIView-Encapsulated-Layout-Height' V:[CustomCell:0x12c751bb0'cell'(250)]>"
)"

1 个答案:

答案 0 :(得分:0)

你有一个额外的约束。取代

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[visualEffectView(50)]|" 
options:0 metrics:nil views:@{@"visualEffectView" : self.visualEffectView}]];

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[visualEffectView(50)]-0-|" 
options:0 metrics:nil views:@{@"visualEffectView" : self.visualEffectView}]];