1

I have a two images with the same constraints and anchors. and when I downscale one of them I get empty bottom space between parent view(red background) and UIImageView(green background). Left image has scale is 100%, right image is 80%

enter image description here

code for constraints:

imageView.heightAnchor.constraint(equalToConstant: 32).isActive = true
imageView.widthAnchor.constraint(equalToConstant: 32).isActive = true
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0).isActive = true
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true

downscale code:

imageView.transform = CGAffineTransform(scaleX: scale, y: scale)

how can I move downscaled image to bottom line of view to avoid space between UIView and UIImageView botoms ?

UPDATE:

Also set imageView.contentMode = .bottom doesn't help

enter image description here

Atlantis
  • 725
  • 7
  • 26

2 Answers2

1

Use UIImageView.contentMode:

imageView.contentMode = .bottom

Read more about content modes here.

Edit:

You shouldn’t be scaling the UIImageView. What you’re doing is kind of strange because you’re scaling it down but then scaling it up again with constraints. Instead, scale the image itself.

Daniel
  • 3,188
  • 14
  • 34
  • I tried but this mode just made image smaller and didn't reflect to UIImageView. I update my question – Atlantis Jul 31 '19 at 04:00
  • Check out my edit. Also, what exactly do you want? Do you want the image at the bottom of the view or the green view at the bottom of the red view? Why not just have the red view be the image view? – Daniel Jul 31 '19 at 08:09
0
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: imageView.bounds.height - imageView.frame.height).isActive = true
}

before

after re layout view and after transform call view.layoutIfNeeded()

Nurka
  • 1
  • 3