Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top