iOS8 - constraints ambiguously suggest a height of zero

前端 未结 18 1691
青春惊慌失措
青春惊慌失措 2020-12-02 14:15

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 c

相关标签:
18条回答
  • 2020-12-02 14:24

    What can also be done is adding vertical constraints from the top and to the bottom of the content view. This will make autolayout happy (because he now knows how to calculate the height of the cell himself).

    0 讨论(0)
  • 2020-12-02 14:26

    You can use AutoLayout to calculate the right height for you. Here is a nice post about Dynamic Cell Height on iOS 8: http://natashatherobot.com/ios-8-self-sizing-table-view-cells-with-dynamic-type/

    0 讨论(0)
  • 2020-12-02 14:27

    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
    0 讨论(0)
  • 2020-12-02 14:27

    In my case, it is because I'm designing the cell with xib, and I forget to add that xib file to the target.

    After I add that xib file to the target, the problem is gone

    0 讨论(0)
  • 2020-12-02 14:27

    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.

    0 讨论(0)
  • 2020-12-02 14:28

    Forcing a return height and estimated height made the warning disappear in my case.

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

    Another solution where you don't need the two overrides is simply to use self.tableView.rowHeight = 44; in your loadView or init method.

    0 讨论(0)
提交回复
热议问题