Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Image View - Height equal to the size of width

Tags:

xcode

ios

I'm just to create an image that spans the entire screens width. The image is a square (512x512) and the idea is to scale to the width of the device while maintaining aspect ratio. I've tried doing this with contraints, setting the margins to left: 0, top: 0, right: 0 and keeping the image at aspect-fill. The issue however, is that the image is becoming larger than the screens width, and thus the image is not only being cut off on the left and right, but is much taller than it should be.

How can I properly size an image to have the same width/height has the width of the device?

like image 940
Hobbyist Avatar asked Mar 13 '23 16:03

Hobbyist


1 Answers

The problem is you didn't set constraints on the image view. Set the following constraints on image view:

1) Set width of image view equal to width of self.view

2) Set aspect ratio 1:1 // This makes sure your image's height is equal to width

You would need additional constraints to specify the x and y position of the image view.

like image 69
Ankit Goel Avatar answered Mar 23 '23 14:03

Ankit Goel