繁体   English   中英

调整UITableViewCell的大小导致NSLayoutConstraint错误

[英]NSLayoutConstraint errors with resizing UITableViewCell

我有一个带有子视图的自定义UITableViewCell,该子视图使用“自动布局”来调整大小。 该子视图是位于单元格底部的工具栏。 取消选择单元格时,它的高度为零,在选定状态下它的高度为30。 单元格在100和130之间切换。

这是初始化:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _toolbarView = [[FOToolbarView alloc] init];
        _toolbarView.translatesAutoresizingMaskIntoConstraints = NO;
        _toolbarView.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview: _toolbarView];

        [self setNeedsUpdateConstraints];
    }
    return self;
}

这是约束

- (void)updateConstraints
{
    [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _toolbarView
                                                                  attribute: NSLayoutAttributeWidth
                                                                  relatedBy: NSLayoutRelationEqual
                                                                     toItem: self.contentView
                                                                  attribute: NSLayoutAttributeWidth
                                                                 multiplier: 1.0
                                                                   constant: 0]];
    [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _toolbarView
                                                                  attribute: NSLayoutAttributeLeft
                                                                  relatedBy: NSLayoutRelationEqual
                                                                     toItem: self.contentView
                                                                  attribute: NSLayoutAttributeLeft
                                                                 multiplier: 1.0
                                                                   constant: 0]];
    [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _toolbarView
                                                                  attribute: NSLayoutAttributeTop
                                                                  relatedBy: NSLayoutRelationEqual
                                                                     toItem: self.contentView
                                                                  attribute: NSLayoutAttributeTop
                                                                 multiplier: 0.0
                                                                   constant: 100.0]];
    [self.contentView addConstraint: [NSLayoutConstraint constraintWithItem: _toolbarView
                                                                  attribute: NSLayoutAttributeBottom
                                                                  relatedBy: NSLayoutRelationEqual
                                                                     toItem: self.contentView
                                                                  attribute: NSLayoutAttributeBottom
                                                                 multiplier: 1.0
                                                                   constant: 0]];
    [super updateConstraints];
}

布局按预期工作,但出现以下错误:

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:0x1f810f50 FOToolbarView:0x1f812900.top == + 70>",
    "<NSLayoutConstraint:0x1f810dc0 FOToolbarView:0x1f812900.bottom == UITableViewCellContentView:0x1f8286d0.bottom>",
    "<NSAutoresizingMaskLayoutConstraint:0x1f821230 h=--& v=--& V: [UITableViewCellContentView:0x1f8286d0(69)]>"
)

我尝试了很多事情,但没有成功。 如何摆脱这个错误?

问题似乎是系统自动添加了来自tableviewcell.contentview的autoresizingmask约束。 尝试这个:

self.contentView.translatesAutoresizingMaskIntoConstraints = NO;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM