Alignment of the View using AUTOLAYOUT AND SIZECLASSES - ios

How to align the trailing edge of the one view to the horizontal centre of the other view.
._____________
|1____________|
.______________________________
|2_____________________________|
I want something like above. I always want the view1 to the half of the width of the view2.

Click both UI elements you want to align and align their trailing edges
Select one of the two elements and double click on the newly created constraint
Change the Edge of the item that you want to be bigger (in my case SecondItem) and set its edge to be CenterX
update the frames and voila

Hold CTRL then click and drag from view1 to view2. Let go of the mouse click and a menu should pop up. Choose both "Left" and "Equal Widths".
Then click the edit button of the Equal Width constraint and change the multiplier to 0.5 (50%).

Related

How to use auto layout for a label in interface builder

How would I go about setting constraints for my price label to stay on the side of the screen? Below Is a picture of what I want my tableviews to look like on all iPhones.
http://imgur.com/a/g6PhL
It's easy to do:
1)first add left label
2)ctrl + drag the left label to cell content, and choose Center Vertically in Container:
3)Click Add New Constraints in the right bottom of interface builder,
deselect the Constraint to margins, and click the left dotted line and set the left margin value , eg:8
4) As the same to left label, you can set constraints to your right label, and the different is only you should set right margin nor left margin , in there I omit the Vertically in Container step of right label, you can see 2):
EDIT
as matt's advice, you better set the right label's alignment to right;
The blue label is on the right and needs to stay on the right. It is sized automatically by its content. So just pin its right edge to the right of its superview and its top or bottom (not both) to the top or bottom of its superview.

Xcode adding constraints so an ImageView

I'm making an app in Xcode where there is a trunk in the middle of the screen and a branch on the right as you can see in the image (I've put two buttons which represent the trunk and the branch. The blue button is the trunk while the red one is the branch).
Using the constraints I managed to resize the trunk properly in every iPhone device, but I am not able to do the same with the branch.
Can you tell me how to keep this configuration of the trunk and the branch using constraints?
I've tried adding:
-Align horizontally in container, using the "Current canvas value"
-Aspect ratio
-Aspect ratio in proportion with the Trunk
but it doesn't work
Here is the image of the buttons which represents the trunk and the branch:
You can do this with 8 constraints:
BlueButton.Top = Superview.Top.Margin
BlueButton.Bottom = BottomLayoutGuide.Top
BlueButton.Width = Superview.Width with multiplier 3:10
BlueButton.CenterX = Superview.CenterX
RedButton.Leading = BlueButton.Trailing
RedButton.Trailing = Superview.Trailing.Margin
RedButton.Width = RedButton.Height with multiplier 10:3
RedButton.Bottom = Superview.Bottom with multiplier 0.4 (to make branch 60% of way up the tree)
To create constraints 1 and 2:
Click on the blue button to select it. Click on the Pin [-[]-] icon. Turn on the top and bottom orange I-beams and click [Add 2 Constraints].
To create constraint 3:
control-drag from blue button to white background. Choose Equal Widths from the pop-up. To set the multiplier, click again on the blue button and find the Equal width to superview constraint in the Size Inspector on the right. Click on Edit and set the multiplier to 3:10.
To create constraint 4:
control-drag from blue button to white background. Choose Center Horizontally in Container from the pop-up.
To create constraint 5:
control-drag from blue button to red button. Choose Horizontal Spacing from the pop-up.
To create constraint 6:
control-drag upwards at a 45 degree angle from center of the red button to the right edge of the background. Choose Trailing Space to Container Margin from the pop-up.
To create constraint 7:
Completely inside the red button, control-drag up and to the right. Choose Aspect Ratio from the pop-up. Find the Ratio constraint in the Size Inspector, click on Edit and set the multiplier to 10:3.
To create constraint 8:
This last one is the hardest, but you can do it! Click on the red button. Now, in the Document Outline to the left of the Storyboard you'll see that the button is highlighted in gray. Hold down the command key and click on the View that contains the button. It should look like this when both the view and the button are highlighted:
Now, click on the Align Icon (to the left of Pin [-[]-]), click the checkbox for Bottom Edges, and click [Add 1 Constraint].
Now, click on your red button again, and find the Bottom Space to: Superview constraint in the Size Inspector. Double click on it to open it up:
This is what we want it to look like. You'll probably have to change 2 things. If your first item is Superview.Bottom then you'll need to swap the first and second items. To do that, click on Superview.Bottom and choose Reverse first and second items.
Next, change the multiplier to 0.4 This means that the red button's bottom is 40% of the way from the top as the Superview's bottom, which makes the red button 60% up from the bottom of the screen.
Whew! And that's it!
First give a height to your branch button.
Then give leading space to trunc button zero and trailing space to superview zero.
Now only one more information is required for xcode to function autolayout properly.i.e.the y postion of branch button.
if you want your branch position to be vertically centre
Give vertically centre in container constaint to branch button

Arrange 3 UIButtons(of equal width) side-by-side

How to arrange 3 UIButtons side-by-side.I am using Auto-layout. My requirement is:
1.they should be equal width regardless of device
2.occupy vertically from starting to end of the view
i tried different ways, i failed to achieve that. Is it possible through interface builder
There are 2 ways to do this.
Way 1:
With use of equal width constraint of button
Select all 3 buttons and add
top, left , right, height and equal width
Way 2:
With use of Stack view
Step 1: Add 3 buttons.
Step 2: Select all that buttons, Once you selected, click on the Stack button in the Auto Layout toolbar at the bottom right of the storyboard canvas. see below in image.
Alternatively you can embed in From Editor -> Embed in -> StackView
Step 3:
Add Constraints to StackView. like below.
Step 4:
Select StackView, Once selected go to Attributes inspector. Change the Distribution to Fill Equally:
And its Done!
Select 3 buttons and give this constraints
- leading
- trailing
- bottom
- Height
- equal width
You can check some references from equal width to 3 labels and from setting two buttons of equal width, side by side
Set the constraints as following :
Select all button and set the equal width constraint, this constraint will keep the width of all the buttons same.
Select all buttons and set the bottom margin, this will keep all the button at bottom of the screen in all form factors.
Set the left margin and right margin of first and last button respectively, with the superview.
Select the button in the middle and set left margin and right margin constraint.
It is possible through interface builder. This is what you do
Create a UIViewController from your Storyboard by clicking the Object Library button.
Now select and drag three buttons and add them to your View Controller. Be sure to add the buttons through here. Drag and drop in the View of your View Controller. Be sure to place the buttons at the bottom of your View Controller.
After your three buttons are added , it should look something like this
Now comes the interesting part. Now what you need to do is add constraints to your buttons so that they stay always at the bottom and of equal width. For simplicity let me call the three buttons as left , center and right button. To keep the button at the button , select the center button and press CONTROL on your keyboard and drag the button to the View. Select the constraint
Vertical spacing to Bottom Layout Guide
and keep a constant = 0. Now for the other two buttons you do this. Simply press CONTROL and drag to the central button. Select the constraint
Bottom
and keep constant for that constraint as 0.This ensures that all the buttons stay at the bottom.
Add the Height constraint as much as you want for all the three buttons.
At the end, these are the constraints you must have.
Center Button Constraint
Left Button Constraint
Right Button Constraint
Now all you need to do is Control+drag the Width constraint of your center button to your ViewController.swift file and change the constant of the width to
widthConstraint.constant = UIScreen.mainScreen().bounds.width/3
And that's it.

Need assistance setting two buttons of equal width, side by side Autolayout

Using auto layout I am trying to create two buttons on the bottom like this
After dragging two buttons I set constraints like this
On Back button I set Leading Space to Container Margin and Bottom Space to Bottom Layout Guide
On Go To Settings button I set Trailing Space to Container Margin and Bottom Space to Bottom Layout Guide
Then I ctrl+Drag from Back button to Go To Settings button and set the Equal Widths constraint and I get this.
Then I update the Back button width same as Go To Settings button which is 101 and all constraints turn to blue.
But I want both buttons to cover half of the screen no matter what the size of screen is, same as shown in first screen shot, How to accomplish this?
you can done it using equal width to 2nd button like this
and the output is
Here is apple guide
You can have the equal widths constraint between the buttons, but you also have to tell them which is that width you need.
So drag from a button to the superview, add an equal widths constraint. Then in the properties of the constraint you've added, set the multiplier to 0.5, this way your buttons should be on half the screen.
You need to add one more constraint - set horizontal space between buttons to 0
You can followed the layout changes. you will get what you want.
Layout for Back button.
Layout for Go to Setting button.

Keep UIButtons centered using autolayout in storyboard

I have two buttons contained within a view (to be precise, a UITableView footer):
I would like to keep the buttons centered (with the gap in-between) regardless of the width of the superview (which will increase, for example, if the device is rotated to portrait orientation).
Is there a way to define these autolayout constraints purely in the storyboard, or will I need to use NSLayoutConstraints in code?
You can achieve that by adding a constraint for each button to align the leading/trailing to the center of the container view, plus some separation between them.
How to do it:
For the left side button, add a constraint between the button and superview to center it horizontally.
Edit the constraint and change the "Second Item" attribute to be button1.trailing, and set a constant of 10 (or whatever distance you want between the right border of the button and the center of the screen).
For the right side button, add a constraint between the button and superview to center it horizontally.
Edit the constraint and change the "Second Item" attribute to be button2.leading and set a constant of -10 (or whatever distance you want between the left border of the button and the center of the screen).

Resources