简体   繁体   中英

Button autosize with auto layout in Xcode 6 w/ swift

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

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-

  1. Select you ViewController and then select "Stimulated Size" in the inspector. There you will see your Stimulator's width and height. In my case, it is 400X600. You can change it to anything, and calculate your proportionality value accordingly.

在此处输入图片说明

  1. Now, we need to position our button. I am just setting it at the middle of the frame. So, hold the "Ctrl" button in the keyboard, click and drag the mouse pointer from the button to the immidiate parent view which will give you a list of options.

在此处输入图片说明

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.

  1. This is the part, where you will have your answer. To set the button size proportionally, you can use aspect ratio.

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 .

  1. Do the 3rd step again. Select aspect ratio again, and select this constrain to edit. However, this time set your first Item as Button's height and your second item as superview's height or vice versa and change the multiplier to 1:12 or 12:1 accordingly.

在此处输入图片说明

Great, if you have followed unto this point then you are done. You should see something like above.

You need following three constraint

  1. For X position : Leading Space to SuperView
  2. For Y position : Align Center to Y
  3. For Width : Trailing Space to SuperView

Check ScreenShow

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM