Question

I have a custom UITableViewCell with a subview that resizes using Auto Layout. This subview is a toolbar at the bottom of the cell. It has a height of zero when the cell is unselected and grows to 30 in the selected state. The cell toggles between 100 and 130.

Here is the 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;
}

And here are the constraints

- (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];
}

The layout works as intended, but I am getting the following error:

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

I've tried many things, without success. How can I get rid of this error?

Was it helpful?

Solution

Problem seems to be that autoresizingmask constraints from tableviewcell.contentview is being automatically added by the system. Try this:

self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top