YungCheng Su
YungCheng Su

Reputation: 193

Making ImageView Depend on Image Size But not Fixed ImabeView

I want to make a dynamic imageView which can depend on the image ratio inside(image's ratio different). And the imageView width follow the screen width(or the tableView width), image's height is resize by the image inside. My storyboard looks like that:enter image description here

enter image description here The imageView mode set Aspect Fit. How can the image's width be the same with tableView(or screen width)? And to resize both the height of the image and tableView ??(don't leave yellow margin)

Upvotes: 1

Views: 564

Answers (2)

Salman Ghumsani
Salman Ghumsani

Reputation: 3657

Don't fix the height and width of imageView. Using constraints just set the x, y position, it automatically becomes resizable according to image size.

Upvotes: 1

RufusV
RufusV

Reputation: 407

I suggest you add a Y constraint, e.g. use the pin board to constraint the vertical spacing to top layout guide, and then use control drag to the view controller icon, to centre the imageView horizontally, then when you set the image for the imageView, it should automatically resize to the size that the image is as you won't have set any size or aspect ratio constraints. If you wanted, you could set a constraint to say you always want it to be an equal height from the table view, however, this could cause the image to look stretched if you do not set the aspect ratio.

Upvotes: 0

Related Questions