title: Auto Layout on UIStackView with UIImageView inside - iOS Swift
body: <p>I have some issues with auto layout when building view programmatically.</p>
  
  <p>There is a vertical UIStackView with several elements inside, mainly labels and images. After setting properties on stackView and imageView, as a result I get something similar to image 1 below (white spaces on top and bottom of the image; the smaller screen size the bigger white spaces) while I would like to get something more similar to image 2 (without white spaces around image).</p>
  
  <p>1: </p>
  
  <p><a href="https://i.stack.imgur.com/Qt5YQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qt5YQ.png" alt="White spaces around image"></a></p>
  
  <p>2:</p>
  
  <p><a href="https://i.stack.imgur.com/1xSXS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1xSXS.png" alt="No white spaces around image"></a></p>
  
  <p>I was reading some tutorials on how to properly set the stackView, its distribution and alignment and as a result in my StackView properties are set like this:</p>
  
  <pre><code>myStackView.axis = .vertical
  myStackView.distribution = .fill
  myStackView.alignment = .fill
  myStackView.spacing = 12
  myStackView.contentMode = .scaleAspectFit
  </code></pre>
  
  <p>and on ImageView before I add it as an arranged subview, I set:</p>
  
  <pre><code>myImageView.contentMode = .scaleAspectFit
  myImageView.autoresizesSubviews = true
  myImageView.sd_setImage(with: URL(string: image))           
  myImageView.setContentHuggingPriority(251, for: .horizontal)
  myImageView.setContentHuggingPriority(500, for: .vertical)
  myImageView.setContentCompressionResistancePriority(750, for: .horizontal)
  myImageView.setContentCompressionResistancePriority(750, for: .vertical)
  </code></pre>
  
  <p>I did not change CHP and CCRP on labels above and under the imageView.</p>
  
  <p>I was trying to manipulate with content hugging priority and content compressions resistance priority but it did not change anything. Any ideas what I do wrong?</p>