2014-01-28 49 views
1

我有一个自定义的UITableViewCell与调整使用自动布局调整的子视图。该子视图是单元格底部的工具栏。当单元格未选中时,它的高度为零,并在选定状态下增长到30。小区切换100和130NSLayoutConstraint错误与调整大小UITableViewCell

之间。这里是init:

- (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)]>" 
) 

我试过很多东西,没有成功。我怎样才能摆脱这个错误?

回答

2

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

self.contentView.translatesAutoresizingMaskIntoConstraints = NO; 
+0

这工作。然后,我只需要在updateConstraints中为contentView添加约束条件,所以它会随着单元格一起扩展。 – Daniel