0

I have a subclass of a UITableViewCell, in the cell I have a UIImageView with a content mode set to ScaleAspectFit. The UIImageView has a static height and uses equal leading & trailing constraints, it also has a top constraint.

Directly below the UIImageView, I have a UIView that I'd like the width to be equal to the width of the scaled image. Ideally I'd like to set a minimum width as well.

I seem to be having trouble getting the size of the image in the image view.

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

extension ViewController: UITableViewDataSource, UITableViewDelegate {

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 1
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    guard let cell = tableView.dequeueReusableCellWithIdentifier("sampleCell") as? SampleCellTableViewCell else {

        return UITableViewCell()
    }

    return cell
}
}

// the cell

class SampleCellTableViewCell: UITableViewCell {

@IBOutlet weak var theImage: UIImageView!
@IBOutlet weak var theView: UIView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
}

enter image description here

I've tried many things to this point so would like a fresh idea if you have one, thank you.

Fred Faust
  • 6,696
  • 4
  • 32
  • 55

1 Answers1

0

The solution ended up being to add a width constraint to the view then creating an outlet for it and changing it's constant value to the width of the scaled image within the image view.

// cell

class SampleCellTableViewCell: UITableViewCell {

@IBOutlet weak var theImage: UIImageView!
@IBOutlet weak var theView: UIView!
@IBOutlet weak var theViewWidthConstraint: NSLayoutConstraint!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}
}

// cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    guard let cell = tableView.dequeueReusableCellWithIdentifier("sampleCell") as? SampleCellTableViewCell else {

        return UITableViewCell()
    }

    cell.theViewWidthConstraint.constant = cell.theImage.frameForImageInImageViewAspectFit().width

    return cell
}

You can find the frameForImageInImageViewAspectFit() method definition here: https://stackoverflow.com/a/36428745/4096655

enter image description here

Community
  • 1
  • 1
Fred Faust
  • 6,696
  • 4
  • 32
  • 55