Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageView resizing to eliminate blank space IOS

My UIImageView is in aspect fit mode which leaves blank space beneath the actual image.

UIImageView displayment

The blue space is what I want to eliminate so the imageView wraps the image (if only I had wrap content). Seemed like an easy task but I soon realized that the image is in aspect fit so the image view changes the width and height of the image but then I would have to make a calculation on the image view based on that altered image which would make a never ending loop of shrinking the image.

UIImageView in the app

I just want to remove the blank space to make it look more concise. Full project at GitHub: https://github.com/sak6lab/Closet-Space

like image 367
Sak6lab Avatar asked Dec 23 '22 16:12

Sak6lab


1 Answers

I filed a radar recently for this as I believe it is broken behaviour.

Currently, the best way I have found to fix this is to add an aspect ratio constraint to the image view based on the aspect ratio of the image. (You'll have to update the constraint when the image changes though).

Something like...

imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor, multiplier: image.size.width / image.size.height).isActive = true

You could wrap this into a UIImageView subclass maybe. Override the didSet of the image property or even just add a new function.

Be careful though as you need to make sure you only create one constraint and on subsequent passes you update the already existing constraint.

like image 105
Fogmeister Avatar answered Jan 07 '23 12:01

Fogmeister