0

I'm using UIView+Autolayout, to make code-based Auto Layout constraint creation easier, but I'm having trouble adding a constraint to a UITableViewCell sub class.

I have a subview ("viewUser") in the cell to group contact information about who posted the other content being shown in the cell. "viewUser" is a fixed height and is always at the bottom of the cell:

enter image description here

To achieve this in the init method I create "viewUser" and add it to the cell's contentView:

self.viewUser = [UIView newAutoLayoutView];

[self.contentView addSubview:self.viewUser];

and in the updateConstraints method I add the following constraints:

[self.viewUser autoSetDimension:ALDimensionHeight toSize:kContentHeight];
[self.viewUser autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0.0f];
[self.viewUser autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0.0f];
[self.viewUser autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0.0f];
[self.viewUser autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0.0f];

When I then create the cells, I get the following warning in the console:

    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)  (
    <NSAutoresizingMaskLayoutConstraint:0xb2a6b40 h=--& v=--& V:[UITableViewCellContentView:0xb289e20(44)]>",
    "<NSLayoutConstraint:0xb2a3360 V:[UIView:0xb2a5fd0(53)]>",
    "<NSLayoutConstraint:0xb2a3420 V:|-(0)-[UIView:0xb2a5fd0]   (Names: '|':UITableViewCellContentView:0xb289e20 )>",
    "<NSLayoutConstraint:0xb2a3840 UIView:0xb2a5fd0.bottom == UITableViewCellContentView:0xb289e20.bottom>" )

Will attempt to recover by breaking constraint  <NSLayoutConstraint:0xb2a3360 V:[UIView:0xb2a5fd0(53)]>

Which to me makes sense because I'm saying that "viewUser" should have a fixed height and then I pin it to the contentView's edges however if I remove:

[self.viewUser autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0.0f];

When I work out the cell's dynamic height in my tableview controller, its height comes back as 0.

I can't work out what I'm doing wrong and how I remove this warning and get the cell's height, any insights would be great.

1 Answer 1

1

It's difficult to diagnose you're problem without knowing the entire layout for your cell. Most likely, you're returning the incorrect height in

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

The height you're returning is too small for both "viewUser" and the content subview above.

If you're "viewUser" is disappearing, there are two scenarios I can think of right now that can result from your table cell height being too small.:

  1. You're height constraint for viewUser was broken automatically at runtime due to constraint conflicts. This is what happens in the warning you posted: cell height was too small (44) for your viewUser height (53)

  2. You're viewUser's height is superseded by a higher priority constraint possibly from the content view. This is less unlikely but is also a possibility.

1
  • 2
    The problem is most definitely #1 here. To fix this, do two things: 1. Make the cell really big to start out, something like cell.bounds = CGRectMake(0, 0, 9999, 9999) so you have plenty of "room" to work with. Then, 2. Change at least 1 of your vertical constraints to the top/bottom edges of the cell to an inequality (specifically, greater than or equal). e.g. [self.viewUser autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0.0f relation:NSLayoutRelationGreaterThanOrEqual];. This prevents an exception if the cell is taller than required (and of course works fine at the fitting size).
    – smileyborg
    May 9, 2014 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.