1

The problem that I have is that somehow the UIView gets an undesirable padding. It seems that the UIView frame resizes itself based on the UIImage height.

These are my constraints on the UIView

Illustrative GIF

Cell Configuration:

// Configure the cell...
    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: cell.testView.frame.width, height: cell.testView.frame.height))

    imageView.image = UIImage(named: "eiffel.JPG")
    imageView.autoresizingMask = .FlexibleWidth
    imageView.contentMode = .ScaleAspectFit
    imageView.center = cell.testView.center
    imageView.clipsToBounds = true
    imageView.translatesAutoresizingMaskIntoConstraints = false


    cell.testView.addSubview(imageView)

    let bottomMarginConstraint = NSLayoutConstraint(item: imageView, attribute: .Bottom, relatedBy: .Equal, toItem: cell.testView, attribute: .Bottom, multiplier: 1, constant: 0)

    let topMarginConstraint = NSLayoutConstraint(item: imageView, attribute: .Top, relatedBy: .Equal, toItem: cell.testView, attribute: .Top, multiplier: 1, constant: 0)

    let trailingMarginConstraint = NSLayoutConstraint(item: imageView, attribute: .Left, relatedBy: .Equal, toItem: cell.testView, attribute: .Left, multiplier: 1, constant: 0)

    let leadingMarginConstraint = NSLayoutConstraint(item: imageView, attribute: .Right, relatedBy: .Equal, toItem: cell.testView, attribute: .Right, multiplier: 1, constant: 0)

    //let widthConstraint = NSLayoutConstraint(item: imageView, attribute: .Width, relatedBy: .Equal, toItem: .None, attribute: .NotAnAttribute, multiplier: 1, constant: imageView.superview!.frame.width)

    let constraints = [bottomMarginConstraint, topMarginConstraint, leadingMarginConstraint, trailingMarginConstraint]

    cell.testView.addConstraints(constraints)

The UIView is the one with the light green background and the Cell the one with the pink background.

Andre Simon
  • 665
  • 10
  • 16

3 Answers3

1

It might be due to the margin that are added by default while using auto layout. Uncheck it from the storyboard.

  • Uncheck "Constrain to margin"

Method 1

Or

  • Uncheck "Relative To Margin"

Method 2

  • Try to remove programatically

imageView.layoutMargins = UIEdgeInsetsMake(0, 0, 0,0);

See More: iOS8 Auto layout programatically pin to relative layout margin

Apple Doc Autolayout

Community
  • 1
  • 1
HDdeveloper
  • 4,396
  • 6
  • 40
  • 65
  • This didn't work for me! I uploaded my project here -> https://www.dropbox.com/s/ikmvtgk6gs12epl/UIImageViewRatioFullSizeTest.zip?dl=0 please help! I reeeeeally don't know what to do now :( – Andre Simon Dec 11 '15 at 14:00
0

I think you can draw the UIImageView in xib too. The size of UIImageView will be changed too when the image changed in Auto layout. You just need to confirm the position of UIImageView.

When tableView need to know the height of this cell, return cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingExpandedSize).size.height + 1 to calculate the height of Cell's contentView and 1.f of seperator line

Klein Mioke
  • 1,261
  • 2
  • 14
  • 23
-2

What I ended up doing was to resize the image based on it's width and then modify the height constraint based on the recalculated height.

Andre Simon
  • 665
  • 10
  • 16