8

I am implementing a functionality in which all labels of my cell has been resized as per the text containing with each label.I have implemented this functionality with the help of RayWanderlich tutorial and its working fine for iPhone 4s,5 and 5s with iOS 8.1. as shown in image below which shows iPhone 5s simulator with iOS 8.1

enter image description here

while I am running my app with iPhone 6 or iPhone 6 plus I got unexpected behavior as shown below image which shows iPhone 6 simulator

enter image description here

and iPhone 6 plus simulator.

enter image description here

I have created two custom cell nib files one for iPhone 4,4s,5 and 5s and second one is for iPhone 6 and 6 plus. As I am new with auto layout I have set constraints same both nibs. I am showing both custom cell nib files with set constraints at below images.

iPhone 4,4s,5 and 5s nib file snapshot

enter image description here

iPhone 6 and iPhone 6 plus Custom cell nib file snapshot

enter image description here

I also used following code snippet from where cell has been resized with dynamic height. The code works for iPhone iPhone 4(for iOS 7),4s,5 and 5s (for iOS 7 and iOS 8 and 8.1) fine but not for iPhone 6 and iPhone 6 plus.

Code Snippet

- (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
}

Please give me a proper solution where am I going wrong.I have surfed lot of things but unable to get proper way to solve this issue. Your help would be appreciable.Thanks in advanced.

Jekil Patel
  • 403
  • 4
  • 18

2 Answers2

2

According to your question of cell height in iPhone6,6 Plus you need to write below lines.

tableView.estimatedRowHeight = 44.0 
tableView.rowHeight = UITableViewAutomaticDimension 

Dynamic Cell Size using auto layout reference: http://www.appcoda.com/self-sizing-cells/

  • it seems it's compulsory to set `estimatedRowHeight` with a value not just `UITableView.automaticDimension` for iPhone 6 and 6 plus – Yahia Dec 03 '19 at 21:13
1

Is there a reason you have two different xibs for the same cell? The whole point of AutoLayout is that a single layout can be flexible enough to support all screen sizes.

When using auto sizing cells, the key things to remember are:

  • tableView.estimatedRowHeight = <estimated_value>
  • tableView.rowHeight = UITableViewAutomaticDimension
  • Make sure you have a non-ambiguous layout inside of your UITableViewCell subclass and all elements are subviews of .contentView

Your layout looks like a perfect fit for UIStackView. I would suggest using a vertical UIStackView containing your 3 labels. Then a horizontal UIStackView containing the previous UIStackView and the UIImageView. The only NSLayoutConstraints you would need to create would be pinning the outer UIStackView to all sides of .contentView

For more info on UIStackViews, check out my blog post:

http://www.raizlabs.com/dev/2016/04/uistackview/

bsmith11
  • 296
  • 1
  • 3