RahulMishra RahulMishra - 19 days ago 8
Objective-C Question

How to maintain UIView ratio across different devices using Autolayout?

I have one

UIView
in
LaunchScreen
.

enter image description here

I have added following constraint to this
UIView
.

enter image description here

I want my red view always
35%
of the screen.

I have calculated the height of
RedView
as : My
ViewController
height is
667
I calculated the
35%
and assigned to
RedView
.

I want to know that is my approach is correct. As their is very slight change in width of
iPhone6Plus
and huge change in height.


Is my constraint correct for 35:65 screen.

Answer

Instead of giving Aspect ratio you could also give it a multiplier for 35%.

Step 1: Pin RedView from 3 sides, Right-Top-Left

Step 2: Make RedView and ViewController's main view to Equal height.

Step 3: Select RedView's equal height constraint and go to it's attribute inspector. (4th right tab)

Step 4: Change Multiplier value to 0.35.

Now RedView will be always 35% of your main screen in both Orientations.

35% always