Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoLayout Understanding Multiplier

I have a problem with multiplier and cannot understand how this feature works. For example i have view has 6:1 multiplier(To SuperView.Leading) as below.

SuperView.Leading with 6:1 multiplier picture When i change the multiplier to 2:1 it seems like below picture.

enter image description here My question is in the 6:1 relation what does 6 and 1 mean. And also in 2:1 relation what does 2 and 1 mean. Similar consider you have three view like the picture below. Totally there 4 blank areas between subViews and superView. How can i say every blank area must be the SuperView.Width / 6 (and every blank width must be equal) enter image description here

Thanks in advance.

like image 962
OmerArslan Avatar asked Dec 19 '16 06:12

OmerArslan


People also ask

What is Autolayout aspect ratio?

If you select Aspect Ratio for a single item, the width of the item is used as the numerator for the ratio, and the height is used for the denominator. If you select Aspect Ratio for multiple items, Auto Layout chooses the width of one of the items for the numerator and the height of another item for the denominator.

Why do we use Autolayout?

When making responsive designs: Auto Layout is essential for making responsive designs, since it allows you to easily change the size and position of elements based on the screen size. This can save you a lot of time and effort when compared to having to create separate designs for different screen sizes.

What is Autolayout when and where would you use it?

What is Auto Layout? Auto Layout is a constraint-based layout system designed for building dynamically sized user interfaces. It lets you create user interface layouts that dynamically adapt for all screen sizes without manually setting the frame of every view.

How do I change the constraint multiplier in Swift?

Since multiplier is a read-only property and you can't change it, you need to replace the constraint with its modified clone.


2 Answers

When working with autolayout, especially when you are working with proportional layouts, you have to use multiplier.

I have to explain here some mathematics. We know straight line equation.

Y = Mx + C

In above equation. Assume M is your multiplier and C is your Constant.

Thus suppose you have superview (in case of iphone 6s plus) of 414(width) x 736(height) size. On that view suppose you created subview.

Now if you want subview size exacly half of superview size, then just drag two constraints from subview to superview. (i.e. Equal Width and Equal Height)

See this Image

enter image description here

Obviously now you will get an error. just like I'm getting. (See below Image)

enter image description here

Now click on both of the constraints one by one, and use multiplier as 0.5. Then use above straight line equation. Here 0.5 means you want width of subview = superviewWidth / 2.0 i.e. 207 px.

In other words you can provide multiplier as 207:414 also.

Y i.e. subviewWidth = ((M i.e. 0.5) * (x i.e. 414 i.e. superviewWidth)) + (Constant i.e. Zero)

Finally you get subviewWidth = 207 px

Similarly do for height of subview. Provide multiplier 0.5 or 368:736.

When done all things, don't forget to click on subview and update frames.

This way constants and multiplier will works.

like image 64
appleBoy21 Avatar answered Oct 03 '22 06:10

appleBoy21


Multiplier is there for creating Proportional Constraint. Auto Layout calculates the first item’s attribute to be the product of the second item’s attribute and this multiplier. Any value other than 1 creates a proportional constraint.

In your case, 6:1 means multiplier is 6/1 = 6. Which means

view.leading  = Superview.leadingMargin*6

replace : with / - you will understand what it means.

like image 24
PlusInfosys Avatar answered Oct 03 '22 05:10

PlusInfosys