单元格中的动态高度问题在iPhone6和iPhone6 plus中

时间:2014-12-24 07:27:58

标签: ios uilabel autolayout nslayoutconstraint custom-cell

我正在实现一个功能,其中我的单元格的所有标签都已按照包含每个标签的文本进行了调整。我在RayWanderlich tutorial的帮助下实现了此功能,并且它适用于iPhone 4s,5和iOS 8.1的5s。如下图所示,其中显示了带有iOS 8.1的 iPhone 5s模拟器

enter image description here

当我使用iPhone 6或iPhone 6运行我的应用程序时,我出现意外行为,如下图所示,显示 iPhone 6模拟器

enter image description here

iPhone 6 plus模拟器

enter image description here

我创建了两个自定义单元格nib文件,一个用于iPhone 4,4s,5和5s,第二个用于iPhone 6和6 plus。由于我是自动布局的新手,我设置了两个笔尖相同的约束。我在下面的图像中显示了两个带有设置约束的自定义单元格nib文件。

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

enter image description here

iPhone 6和iPhone 6以及自定义单元格nib文件快照

enter image description here

我还使用了以下代码片段,其中单元格已使用动态高度调整大小。该代码适用于iPhone 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
}

如果我出错了,请给我一个合适的解决方案。我已经浏览了很多东西,但无法正确解决这个问题。你的帮助会很明显。谢谢你。

2 个答案:

答案 0 :(得分:1)

根据您在iPhone6,6 Plus中的细胞高度问题,您需要写下以下行。

tableView.estimatedRowHeight = 44.0 
tableView.rowHeight = UITableViewAutomaticDimension 

使用自动布局的动态像元大小 参考:http://www.appcoda.com/self-sizing-cells/

答案 1 :(得分:1)

是否有理由为同一个细胞提供两种不同的xib? AutoLayout的重点在于单个布局可以足够灵活,以支持所有屏幕尺寸。

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

  • tableView.estimatedRowHeight = <estimated_value>
  • tableView.rowHeight = UITableViewAutomaticDimension
  • 确保您的UITableViewCell子类内部有一个非模糊的布局,所有元素都是.contentView
  • 的子视图

您的布局看起来非常适合UIStackView。我建议使用包含3个标签的垂直UIStackView。然后是包含上一个UIStackViewUIStackView的水平UIImageView。您需要创建的唯一NSLayoutConstraints将外部UIStackView固定到.contentView的所有边

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

enter image description here

相关问题