Search code examples
iosswiftconstraintsuitableviewautomaticdimension

UITableViewAutomaticDimension not correctly sizing UIIImageView


I am trying to set the UITableViewCell height based on what is in the cell (Primarily the image). It looks fine on an iPhone 7 Plus, however any smaller devices produce something similar to this.

My code in MainViewController:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

My constraints: enter image description here

What am I doing wrong? I have the height and width constraints set to 'Greater or Equal to' 200. The height and width of the UIImageView on the Storyboard are 370x370


Solution

  • I was able to solve this myself by doing these steps below:

    1. Switching my Storyboard View to iPhone SE
    2. Setting the UIImageView to 'Greater than or Equal' value: 275
    3. Setting the aspect ratio to 1:1 for the width and height of UIImageView

    Here's an image as an example:

    enter image description here