Ios iPhone6和iPhone6 plus手机中的动态高度问题

Ios iPhone6和iPhone6 plus手机中的动态高度问题,ios,uilabel,autolayout,nslayoutconstraint,custom-cell,Ios,Uilabel,Autolayout,Nslayoutconstraint,Custom Cell,我正在实现一项功能,其中我的手机的所有标签都已根据每个标签包含的文本调整大小。我在iOS 8.1的iPhone 4s、5和5s的帮助下实现了此功能,并且该功能运行良好。如下图所示,其中显示了带有iOS 8.1的iPhone 5s模拟器 当我用iPhone 6或iPhone 6 plus运行我的应用程序时,我得到了意想不到的行为,如下图所示,显示了iPhone 6模拟器 和iPhone 6 plus模拟器 我已经创建了两个定制的手机笔尖文件,一个用于iPhone4、4s、5和5s,另一个用于

我正在实现一项功能,其中我的手机的所有标签都已根据每个标签包含的文本调整大小。我在iOS 8.1的iPhone 4s、5和5s的帮助下实现了此功能,并且该功能运行良好。如下图所示,其中显示了带有iOS 8.1的iPhone 5s模拟器

当我用iPhone 6或iPhone 6 plus运行我的应用程序时,我得到了意想不到的行为,如下图所示,显示了iPhone 6模拟器

和iPhone 6 plus模拟器

我已经创建了两个定制的手机笔尖文件,一个用于iPhone4、4s、5和5s,另一个用于iPhone6和6 plus。由于我是自动布局新手,所以我对两个笔尖设置了相同的约束。我在下面的图片中显示了两个自定义单元格nib文件,并设置了约束条件

iPhone 4、4s、5和5s nib文件快照

- (CGFloat)calculateHeightForConfiguredSizingCell_3:(MemberListTableViewCell_WithoutImage_3_iPhone *)sizingCell {


    sizingCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tblView.frame), CGRectGetHeight(sizingCell.bounds));

    [sizingCell setNeedsLayout];
    [sizingCell layoutIfNeeded];

    CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

    //CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize withHorizontalFittingPriority:UILayoutPriorityDefaultHigh verticalFittingPriority:UILayoutPriorityDefaultHigh];

    return size.height + 1.0f; // Add 1.0f for the cell separator height
}

iPhone 6和iPhone 6 plus自定义手机笔尖文件快照

- (CGFloat)calculateHeightForConfiguredSizingCell_3:(MemberListTableViewCell_WithoutImage_3_iPhone *)sizingCell {


    sizingCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tblView.frame), CGRectGetHeight(sizingCell.bounds));

    [sizingCell setNeedsLayout];
    [sizingCell layoutIfNeeded];

    CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

    //CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize withHorizontalFittingPriority:UILayoutPriorityDefaultHigh verticalFittingPriority:UILayoutPriorityDefaultHigh];

    return size.height + 1.0f; // Add 1.0f for the cell separator height
}

我还使用了下面的代码片段,其中单元格的大小已通过动态高度调整。该代码适用于iPhone 4(适用于iOS 7)、4s、5和5s(适用于iOS 7、iOS 8和8.1),但不适用于iPhone 6和iPhone 6 plus

代码片段

- (CGFloat)calculateHeightForConfiguredSizingCell_3:(MemberListTableViewCell_WithoutImage_3_iPhone *)sizingCell {


    sizingCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.tblView.frame), CGRectGetHeight(sizingCell.bounds));

    [sizingCell setNeedsLayout];
    [sizingCell layoutIfNeeded];

    CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

    //CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize withHorizontalFittingPriority:UILayoutPriorityDefaultHigh verticalFittingPriority:UILayoutPriorityDefaultHigh];

    return size.height + 1.0f; // Add 1.0f for the cell separator height
}

请给我一个适当的解决方案,我哪里出了问题。我浏览了很多东西,但无法找到适当的方法来解决这个问题。您的帮助将是值得感激的。非常感谢。

根据您对iPhone6、6 Plus手机高度的疑问,您需要在下面几行写下

tableView.estimatedRowHeight = 44.0 
tableView.rowHeight = UITableViewAutomaticDimension 
使用自动布局的动态单元大小
参考资料:

同一单元格有两个不同的XIB有什么原因吗?AutoLayout的全部要点是,单个布局可以足够灵活,以支持所有屏幕大小

使用自动调整单元格大小时,需要记住的关键事项是:

  • tableView.estimateDroweight=
  • tableView.rowHeight=UITableViewAutomaticDimension
  • 确保在
    UITableViewCell
    子类中有一个不含糊的布局,并且所有元素都是
    的子视图。contentView
您的布局看起来非常适合
UIStackView
。我建议使用包含3个标签的垂直视图。然后是一个水平
UIStackView
,其中包含前面的
UIStackView
UIImageView
。您需要创建的唯一
NSLayoutConstraints
是将外部
UIStackView
固定到
的所有侧面。contentView

有关
UIStackViews
的更多信息,请查看我的博文:


似乎必须为iPhone 6和6 plus设置
estimatedRowHeight
值,而不仅仅是
UITableView.automaticDimension