Xcode adding constraints so an ImageView - ios

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

Related

how draw button story board?

i want to button width = 1
but current image red button : Gray button
2 : 1 how draw buttons -> 1 : 1
First delete all constrains from view controller
Drag Red button from left blue line to center blue line
Drag Gray button from Right blue line to center blue line
create constrains for left button to View leading and top space
create constrains for Right button to View trailing and top space
create constrains for red button to gray button horizontal space
now update frames then check
i don't really know what you want, but if you want two buttons like this picture,here is the answer.
add constraints to left button like this left button constraints
all you need to do is add margin between two buttons and make them equal width.
sorry i cant post more than 2 links.
if you want height and width equal for both items use these constants :-
Use this constraints
Button1(left button) ie. add [left,bottom,horizontal space to right button2,equal height and width to button2 by(draging button1 one towards button2)]
Button 2(right): add [right,and bottom constraint]
update frame is any warning by selecting buttons constraint issue

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.

Alignment of the View using AUTOLAYOUT AND SIZECLASSES

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%).

How to do vertical shrinking of subviews using autolayout when their superview is shrinking vertically?

I would like to be enlightened on the idea: shrinking of superview which would result to automatic shrinking of its subviews also. I want to know how to do this in the Interface Builder.
I still like to deactivate Autolayout for that. (select the View in the IB, in the utilities tab you go to the file inspector and uncheck the "Use Auto Layout") property.
Then:
1. In the interface builder you select the subview.
2. You go to the size inspector (2nd from right, looks like a ruler)
In the middle on the left you have a gray square with 2 arrows in the middle and 4 lines on the outside (The Autosizing box). By clicking them you can activate them (red) or deactivate them. The 4 lines on the outside represent the "stick to the superview". E.g. if you activate the red line on the right the subview will always keep its set distance to its superview. the arrows in the middle activate/deactivate horizontal/vertical autoresize.
So to make a subview stick to its superview in every direction and make it resize alongside you have to activate both arrows and all lines.

How to setup autolayout constraints in Interface Builder so that the default Constant always is 0?

This is for XCode 5, which has sane autolayout generation.
If I drag, say, an UIImageView into a UIIView and want it to be centered in it's superview. I CTRL-drag the UIImageView onto it's superview and then shift-select Center Horizontally in Container as well as Center Vertically in Container.
This creates two constraints for where the constants are set to the current horizontal and vertical offset of the UIImageView. I've never had a case where this is what I wanted.
I then have to manually select each contraints, change the constant to 0, then trigger the view to update its frame.
Is there a way, with some keybind or a preference option, to make it so that I can create constraints that have a zero constant, instead of the view's current offset?
I haven't seen the case you're talking about where you get non-zero constraint values when you choose to add a centerX and centerY constraint -- that may be because we're adding the constraints in a different manner. The way I would add those constraints, would be to drag in the view, then click on the icon (second from left) at the bottom right of the canvas. Click on the check boxes for "horizontal center in container" and "vertical center in container", choose "items of new constraints" from the pull down control at the bottom of that window, and click "add constraints".

Resources