0

在 UITableViewCell 子类中,我向 backgroundView 添加了一个子视图:

-(void)method {
    [self.backgroundView addSubview:self.backView];
    [self setConstraintsForBackView];
}

-(UIView*)backView {
    if (!_backView) {
        _backView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    return _backView;
}

我正在尝试backView使用backgroundView. 我认为使用 NSLayoutConstraints 将是最好的方法,但backView总是以原始的 44 点单元格高度出现,而不是我为每个单元格设置的实际高度。这是我的 NSLayoutConstraints 的代码

-(void)setConstraintsForBackView {
    [self.backView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|"
                                                                          options:0
                                                                          metrics:nil
                                                                            views:@{@"view": self.backView}]];
}

我看到以下错误:

'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:<NSLayoutConstraint:0x89662d0 V:|-(0)-[UIView:0x8e87c10] (Names: '|':UIView:0x8e876c0 )> view:<UIView: 0x8e87c10; frame = (0 0; 0 0); layer = <CALayer: 0x8e87c70>>'

我也试过@"V:|-0-[view]-0-|"没有任何运气。

诚然,我没有大量使用 NSLayoutConstraints 的经验,但是使用各种视觉格式并没有产生任何结果。

4

1 回答 1

0

您需要将约束添加到self.backgroundViewnot self.backView

第一的:self.backView.translatesAutoresizingMaskIntoConstraints = NO;

IE:[self.backgroundView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:nil views:@{@"view": self.backView}]];

于 2013-06-17T20:13:39.060 回答