## Search Your Question

Showing posts with label Auto-Layout. Show all posts
Showing posts with label Auto-Layout. Show all posts

### If there are two views up and bottom, up-view is 60% of superview and 40% of superview respectively, then what to do with constraint?

Ans :

 Use of multiplier in constraint
We took 2 views - Orange and Green.
We set orange view's top, leading, trailing constraint to main view as 0.
Same, we set green view's bottom, leading, trailing constraint to main views as 0.

Now,
We select height constraint of orange view equal to main view's height. And set multiplier 0.6. It shows ratio of orange view's height to main view's height. We have set 0.6 = 60/100 . So orange view's height become 60% of main view's height.

 Set multiplier of height constraint in size inspector

Same we have done with green view and set multiplier 0.4 = 40/100 means 40%.

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

### What is priority in constraints?

Ans :

Constant priority is a number to determine how important is that constraint. The number can range from 1 to 1000, the higher the number goes, the more important a constraint is. Lower priority in screen seen like dashed blue line.

This is useful when two constraint make conflict.

Example :

Let's take one UIView and place them in main view.

Set Leading and Trailing constraint is 40,40.
Set width and height is 240,240.

Now if iPhone SE is selected, then all goes fine. No red line will show about conflicting constraints due to iPhone SE width is 320. So if it takes 40,40 as leading and constraints, it will calculate view's width as 240. And we also set width constraint as 240. So there is no conflicting.

But if iPhone 8 is selected then view's width constraint is conflicting with leading, trailing constraints.
iPhone 8 width = 375 - 40 - 40 = 295 But we have set width constraint as 240. So here conflicting occurs.

Solution :

If we set width constraint priority 900 then its priority is lower than leading and trailing priority(1000) So width constraint will be ignored here. So when we do some auto layout and we feel conflicting constraint, we can lower priority of constraint according to requirement.

### What is multiplier and ratio in auto layout in ios?

Ans :

Difference between Multiplier and Ratio :

There is not any difference. Both are using for same purpose.

The Aspect Ratio is really just a convenient way to express the Multiplier when working in Interface Builder. It effectively gets "converted" to a multiplier.

You can confirm this while debugging by inspecting the constraint's .multiplier property. If you set a view's width to 60, and multiplier to 1:2 (resulting in an auto-layout height of 120), the actual value of .multiplier will be 0.5.

So, in my view, it depends on what feels more natural.

If I want a view to be 90% of the width of another view, I am much more likely to set the Multiplier to 0.9 --- which gives the exact same result as setting it to 9:10.

However, if I want a view to maintain an aspect ration of, say, 3-to-2, I am much more likely to set the Multiplier to 3:2 rather than 1.5.

Using a ratio can also be convenient when you have "non-simple" values. That is, it's easy to understand that a ratio of 3:2 is the same as 1.5. But what if I have an image with native size of 281 x 60, and I want to use those values to maintain ratio? 281:60 is easier to understand than .multiplier = 4.68333339691162.

And, while they are interchangeable, it is probably a bit more intuitive to use Ratio when constraining an object to itself - e.g. I want my view's width to be 2 x its own height, so 2:1 - and using Multiplier when constraining one object to another - e.g. I want my view's width to be 75% of the width of its superview, so 0.75.