Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolayout: origin and size should change according to width and height factor

Here is a scenario what I needed.

I have put a UIButton on IB (size iPhone4 inch) whose initial frame on IB is x:100,y:100,w:100,h:100. I want the origins and height should change according to device size. for example the width and height of iPhone6 is 375X667, hence the width factor (375/320=1.1718) and height factor (667/568=1.1742). In this scene I want to change my buttons origin X and width with multiply by width factor (1.1718) and origin Y and height with multiply by height factor (1.1782). In this case my button should be shown on x:100X1.1718, y:100X1.1742, w:100X1.1718, h:100X1.1742 .

same thing is needed for iPhone6+.

enter image description here

I can easily achieve this by setting This autoresizing masks. How can I achieve the same by using autolayout? What step should I follow, what constraints should I apply?

My previous question on this

I am attaching some screenshots what i needed by using Autolayout (Achieved with autoresizing mask shown in above image).

I am attaching some screenshots what i needed by using autoresizing.

enter image description here

enter image description here

enter image description here

My Try:-

Step1:- Take a button of size 100X100 on IB. set its X=100 and y=100. enter image description here Step2:- applied this constraints. enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

like image 553
Mayank Jain Avatar asked Feb 14 '15 07:02

Mayank Jain


1 Answers

Use the multiplier of the constraints. So, set up a constraint that the button's width equals the top view's width with a multiplier of 100:320 (you can use this ratio form to express the multiplier). Set the constraint's constant to 0. Do the same for button's height proportional to top view's height (ratio 100:568). For the button's position, constrain the button's leading to the top view's width with a ratio of 100:320 and the button's top to the top view's height with a ratio of 100:568.

like image 87
Ken Thomases Avatar answered Oct 14 '22 17:10

Ken Thomases