Nikita Zernov
Nikita Zernov

Reputation: 5635

Image size relative to screen size

I am making iOS view using storyboard and autolayout. I placed image in centre of screen. On all devices (iPhone 4, iPhone 5, iPhone 6 and iPhone 6 Plus) size is the same. It looks too big on iPhone 4 and too small on 6 Plus. How to make image's size relative to screen size. I'd like to use constraints and auto layout here, but all answers appreciated.

Upvotes: 0

Views: 2168

Answers (4)

Bhadresh Mulsaniya
Bhadresh Mulsaniya

Reputation: 2640

Below are the constraints for image view:

enter image description here

Upvotes: 0

Ketan Parmar
Ketan Parmar

Reputation: 27438

There are two options.

1) Give constraints like top, bottom, leading and trailing.

and

2) Give constraints like leading, trailing, top (or bottom which is apropriate) and aspect ratio. (This will keep your imageview's shape same in all size)

Hope this will help :)

Upvotes: 3

Mahendra
Mahendra

Reputation: 8914

You need to surround image view with constraints (Top, Trailing, Bottom, Leading) and don't give height and width constraints to image view as it fix the image view.

The image view will set according to screen size doesn't matter what device it is (iPhone 4, iPhone 5, iPhone 6, iPhone 6 Plus, iPod, iPad, iPad Pro, etc.)

Upvotes: 0

Ghulam Ali
Ghulam Ali

Reputation: 1935

Use these constraints for your UIImageView:

Constraints

It will create 50 trailing space to left, right, top and bottom and will resize according to the screen size. The point is, don't set fixed width and height. Instead set Leading, Trailing, Bottom and Top constraints.

Upvotes: 1

Related Questions