rj2700
rj2700

Reputation: 1965

Swift - Auto constraints

I am trying to use the auto size classes from the storyboard on xcode 7 to position UI elements in a controller. The problem that I'm running into is that when I try to use the "Add missing constraints" function (located at the bottom right corner of the console), it positions my UI elements correctly except for the last elements (pictures describe better). The first image below shows the storyboard file where I just want 3 buttons (stacked above eachother) to be the same width and length to be on the top right corner of any screen.

enter image description here

However, when I add constraints and run the simulations, it seems like the top two buttons are positioned correctly with the correct length and width but the third button is out of place (image below).

enter image description here

So my question is, am I forgetting a step to make all buttons position themselves? Or should I try to convert everything to a percentage and place UI elements based on the percentage of the screen (if so, how would I go about doing that)?

I've also tried adding another blank button (removing the button label) underneath the 3rd button and adding constraints like that but it didn't work for me. Let me know if you have any suggestions, thanks!

Upvotes: 0

Views: 234

Answers (2)

Lastmboy
Lastmboy

Reputation: 1869

You can actually see the solution in action if you use the Preview screen while setting up your auto layout constraints. I just created a similar view and buttons and stepped through the process. I coloured the buttons and named them to make things obvious.

  1. I added the three buttons. At this point, none of the buttons show up in the preview.

enter image description here

enter image description here

  1. I then setup the auto layout constraints for Button1. If you want the buttons anchored to the top right, then you don't need to worry about the leading constraint. You need width, height, top, and trailing.

enter image description here

enter image description here

Now Button1 will snap to it's position in the top right corner of the preview screen.

enter image description here

  1. Now do the same thing for Button2. Set width, height, top (vertical space to Button1), and trailing.

enter image description here

enter image description here

Button2 will now snap into place in the preview.

enter image description here

  1. Now the same thing again for Button3. You anchored the first button to the top right of the screen. Then Button2 to the bottom of Button1 and the right edge of the screen. Then again for Button3. You could also align the edges of the 2nd and 3rd buttons, if you prefer that to trailing space.

enter image description here

enter image description here

Now you'll see in the preview that your buttons are correctly positioned, regardless of device.

enter image description here

As long as you specify height and width for each button, you don't need to worry about the left edge or the bottom edge of the screen at all. They each know to "stick" to the top right and they know what size to be.

** Note: If you're not familiar with the "Preview" option... With your storyboard open, hold Option and select storyboard again to get another copy of the storyboard on the right side. Highlight the view controller you are interested in on the left side. On the right side, select the Preview option as shown below.

enter image description here

Now you have your storyboard and the preview side by side, so you can see the exact impact of any auto layout changes you make. You can also add or remove devices to the preview.

enter image description here

Upvotes: 1

Sohel L.
Sohel L.

Reputation: 9540

In you case, Autolayout the constraints you need to give to UIButton is 4 constrains.

  1. Leading
  2. Trailing
  3. Width
  4. Height

If you miss any of them, then surely you will get an error. So, what's your error is?

To the third UIButton, you have not given the height, while to the above two buttons you have given.

So, just remove the bottom constraint of UIButton and give the equal height to above UIButton.

FYI, never use Add Missing Constraints without any confirmation from your side.

Update:

Check this video to remove trailing or leading margin:

http://sendvid.com/1h8deg18

Upvotes: 2

Related Questions