Search code examples
iosobjective-cnslayoutconstraint

Cant get constraint to pin UIView to bottom of cells contentView


In the cellForRowAtIndexPath: method of a UITableViewController in my application I need to pin a programmatic UIView to the bottom of each cell's respective cell.contentView. Here's my code:

separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320,15)];
separatorLineView.tag = 17;
separatorLineView.backgroundColor = [UIColor colorWithHexString:@"F0F5F7"];
[cell.contentView addSubview:separatorLineView];
[self.separatorLineView setTranslatesAutoresizingMaskIntoConstraints:NO];

NSDictionary* views = NSDictionaryOfVariableBindings(separatorLineView);
NSString *format = @"V:[separatorLineView]-|";


positionYConstraint = [NSLayoutConstraint constraintsWithVisualFormat:format
                                                                  options:0
                                                                  metrics:nil
                                                                    views:views];       

heightConstraint = [NSLayoutConstraint constraintWithItem:postSeparatorLineView
                                                    attribute:NSLayoutAttributeHeight
                                                    relatedBy:NSLayoutRelationEqual
                                                       toItem:cell.contentView
                                                    attribute:NSLayoutAttributeHeight
                                                   multiplier:0.01
                                                     constant:15];


[cell.contentView addConstraints:positionYConstraint];
[cell.contentView addConstraint:heightConstraint];
[cell.contentView layoutSubviews];

When running it with the setTranslatesAuto...:NO line I cannot see the separatorLineView, not even in the view debugger. When I comment that line out, the separatorLineView is set to the top of the cell.contentView.

Essentially all I need the code to do, is pin the separatorLineView to the bottom of each and every cell's contentView - keep in mind that I have dynamic cell heights.


Solution

  • You need constraints on the horizontal, otherwise it won't know its width and x position. Check this:

    UIView * separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320,15)];
    separatorLineView.tag = 17;
    separatorLineView.backgroundColor = [UIColor colorWithHexString:@"F0F5F7"];    [cell.contentView addSubview:separatorLineView];
    [separatorLineView setTranslatesAutoresizingMaskIntoConstraints:NO];
    
    NSDictionary* views = NSDictionaryOfVariableBindings(separatorLineView);
    NSString *formatV = @"V:[separatorLineView(==15)]|"; // changed this line to set 15 as height and to really pin your view (note I remove the "-" because it adds a 8 margin)
    
    
    NSArray *positionYConstraint = [NSLayoutConstraint constraintsWithVisualFormat:formatV
                                                                  options:0
                                                                  metrics:nil
                                                                    views:views];
    
    NSString *formatH = @"H:|[separatorLineView]|"; //these are the missing constraints
    NSArray *positionXConstraint = [NSLayoutConstraint constraintsWithVisualFormat:formatH
                                                                  options:0
                                                                  metrics:nil
                                                                    views:views];
    
    
    [cell.contentView addConstraints:positionYConstraint];
    [cell.contentView addConstraints:positionXConstraint];