2

I have a problem with UITableViewAutomaticDimension:

In my tableview I have multiple imageviews created programmatically with calculated height:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "managePlayset", for: indexPath) as! ManagePlaysetTableViewCell
    let playset = playsets[indexPath.row]
    cell.playsetName.text = playset.name
    if (playset.musicItems?.count)! > 0 {
        var i = 0

        for musicItem in (playset.musicItems?.array)! {

            let imageView = UIImageView(image: (musicItem as! MusicItem).getFirstPhoto())
            imageView.frame = CGRect(x: cell.contentView.frame.origin.x+CGFloat(i)*(imageWidth+10.0), y: cell.contentView.frame.origin.y+40, width: imageWidth, height: imageWidth)
            imageView.contentMode = .scaleAspectFit
            cell.contentView.addSubview(imageView)
            let constraint = NSLayoutConstraint(item: imageView, 
            attribute: NSLayoutAttribute.bottomMargin, relatedBy: 
            NSLayoutRelation.equal, toItem: cell.contentView, 
            attribute: NSLayoutAttribute.bottomMargin, multiplier: 1, 
            constant: 0)
            cell.contentView.addConstraint(constraint)

            i = i + 1
        }
    }

    cell.editButton.tag = indexPath.row
    return cell
}

In viewDidLoad I set:

let width = view.frame.width
    imageWidth = width / CGFloat(maxItemsOnPage) - 10.0
    tableView.estimatedRowHeight = imageWidth + 20.0
    tableView.rowHeight = UITableViewAutomaticDimension

But height is not adjusted properly (images are cut off) Any advice is highly appreciated!

Elena Rubilova
  • 367
  • 4
  • 16
  • You need to add constraints (programmatically since you are adding the `UIImageView`s programmatically) that define the height of the `UITableViewCell` based upon the height of a `UIImageView` (or whatever defines the height of a `UITableViewCell`. This is because `UITableViewAutomaticDimension` works with constraints. – Robotic Cat Apr 23 '17 at 21:21
  • I added constraints programmatically but it doesn't help for some reason – Elena Rubilova Apr 23 '17 at 21:50

2 Answers2

1

I think it's because UIImageView created programmatically. Then it don't use autolayout system. To change it, you have to add imageView.translatesAutoresizingMaskIntoConstraints = false in your loop.

Shmidt
  • 16,436
  • 18
  • 88
  • 136
0

Have you tried constraining both top and bottom edges of the imageView? i.e.

let constraintTop = NSLayoutConstraint(item: imageView, 
        attribute: NSLayoutAttribute.top, relatedBy: 
        NSLayoutRelation.equal, toItem: cell.contentView, 
        attribute: NSLayoutAttribute.topMargin, multiplier: 1, 
        constant: 0)
let constraintBottom = NSLayoutConstraint(item: imageView, 
        attribute: NSLayoutAttribute.bottom, relatedBy: 
        NSLayoutRelation.equal, toItem: cell.contentView, 
        attribute: NSLayoutAttribute.bottomMargin, multiplier: 1, 
        constant: 0)
cell.contentView.addConstraints([constraintTop, constraintBottom])
MishaA
  • 1
  • 1