Reputation: 11
could anyone of you give me a hint, how to implement autosize of a button with auto-layout?
To be more specific: My button has 50x50px at iPhone 4s. How to implement this button, that its size increases on iPhone 6 plus screen resolution? Is there a kind of multiplier option?
Edit: I want, that the button w/ 50x50px on iPhone4s screen is increasing proportionally at iPhone5, iPhone6 display.
Thanks.
BR
Upvotes: 1
Views: 4189
Reputation: 6893
Use aspect Ratio. It's all about math. Before you jump into the action, let's figure out how we are calculating the proportion. So, if you want your button to be 50X50 on a 400X600 frame, how will you find the proportion. Easy- your button's width will be 1/8th of your frame and your button's height will be 1/12th of your frame's height. That's it. Now you have your calculation.
Step By Step Instruction:
First make sure, you have the size class as "wCompact hAny". We should see, something like-
Now we are ready-
From there, when you select the centre horizontally and the centre vertically option your button in the frame should look like this-
Now you can see some orange dots, but ignore that for now.
Again, hold the "Ctrl" button in the keyboard, click and drag the mouse pointer from the button to the immidiate parent view. This time select aspect ratio from the list.
Select the aspect ratio constrain from the constrain list to edit. You need to make sure that your first Item is Button's width and your second item is superview's width or vice versa. Remember, you want to set the button's width's proportionality value(multiplier) in accordance to the frame's width NOT height.
Now, set the multiplier to 1:8 if your 1st item is your button's width, or 8:1 if your 1st item is superview's width.
Great, if you have followed unto this point then you are done. You should see something like above.
Upvotes: 2
Reputation: 13020
You need following three constraint
- For X position : Leading Space to SuperView
- For Y position : Align Center to Y
- For Width : Trailing Space to SuperView
Check ScreenShow
Upvotes: 0