iOS8 - constraints ambiguously suggest a height of

2019-01-10 11:40发布

Has anyone got any idea how to debug this?

Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.

The rows have a fixed height as set by

- (CGFloat)tableView:(UITableView *)tableView 
           heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   return 34.0;
}

And all the constraints seem to be happy...

17条回答
▲ chillily
2楼-- · 2019-01-10 12:27

I used Row Height 43 (or <> 44) in the Table View size inspector and the error disappeared. Using 44 I get the error. Xcode version 6.0.1.

-- This answer was removed by a moderator, please don't, it fixes the problem. This SOLVES the problem for me and may do it for others too. So could you be so kind not to delete it again.

查看更多
劫难
3楼-- · 2019-01-10 12:28

If you're using autoLayout constraints and UITableViewAutomaticDimension, this error is not some erroneous problem to be discarded by overriding your height in code. It means that determining the cell height automatically isn't working because you don't have the proper vertical constraints needed.

If you're like me and were getting this error and needed help identifying which cell was throwing the error, you can add the following line right before the return of your 'heightforRowAtIndexPath' method.

NSLog(@"Section %ld Row %ld", (long)[indexPath section], (long)[indexPath row]);

This will print out a long list of sections and rows, but the error will appear immediately following the particular cell that is causing the error, and you can quickly identify which cell is causing the problem and fix your constraints accordingly. This is particularly helpful for static cells. Overriding the height with a manually entered number will work if you're not using autoLayout and automatic cell heights, but will essentially disable these features which is a very poor solution if its something you're trying to utilize.

If you weren't previously using the 'heightForRowAtIndexPath' method but want to debug this error without undoing your UITableViewAutomaticDimension setting, simply add this to your code:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"Section %ld Row %ld", (long)[indexPath section], (long)[indexPath row]);
    return UITableViewAutomaticDimension;
}
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-10 12:28

If you're getting that warning, it's most likely because you're using auto layout and your cells don't have any constraints inside them.

You should either stop using auto layout or implement constraints that unambiguously define the height of the cells.

You can turn auto layout off in interface builder by unchecking the "Use Autolayout" option in the file inspector on the right.

If you choose to use auto layout and the height of your cells is fixed, implementing the appropriate constraints should be easy. Simply add height constraints for subviews of the cell's content view, and implement vertical space constraints between the subviews, and between the subviews and the content view. For example if your cell has one label in it, this would work:

Vertical constraints

  1. Vertical space constraint between the top of the content view and the top of the label
  2. Fixed height constraint of label
  3. Vertical space constraint between the bottom of the label and the bottom of the content view

Horizontal constraints

  1. Horizontal space constraint between the leading edge of the content view and the leading edge of the label
  2. Fixed width constraint of label
  3. Horizontal space constraint between the trailing edge of the label and the trailing edge of the content view
查看更多
小情绪 Triste *
5楼-- · 2019-01-10 12:30

I couldn't get to remove the warning, but to make constraints work I set the ,new to iOS8 , tableview property estimatedRowHeight to the fixed height, and removed heightForRowAtIndexPath implementation.

查看更多
爱情/是我丢掉的垃圾
6楼-- · 2019-01-10 12:33

I've also seen this error when using universal storyboards or xibs. If you neglect to specify proper constraints for the Any x Any size class, I've seen this error appear.

Apple seems to have fixed this for iOS9. The error only happened on 8.4 for me.

查看更多
太酷不给撩
7楼-- · 2019-01-10 12:38

I was using a mapView inside uitableviewcell. I changed the height of map view to 1/3th of the device screen size. I got the same error. I fixed the error by adding missing constraints to the content view of the uitableviewcell.

1) Clear the contentView constraints.

2) Set Reset to Suggested constants to contentView.

enter image description here

3) Add missing constraints - if any

4) We make sure the content view has all the required constraints. enter image description here

查看更多
登录 后发表回答