Search code examples
iosiphoneautolayout

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.


Solution

  • 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.