How do I use autolayout to align these two UIButtons? - ios

I have a custom UIView XIB file, in which I have placed two buttons:
Image of Buttons Here
How can I progammatically (Swift) or using the interface align these buttons to look like this?
Currently, I get this
Current Buttons (BAD)
Following Alexander's method, i did this, this is my current constraints and workspace.

Add following constraint to Button 1:
Leading space to superview
Equal Width to Button 2
Equal Height to Button 2
Horizontal Spacing to Button 2
For Button 2:
Trailing Space to SuperView
Horizontal Spacing to Button 1
Equal Width to Button 1
Equal Height to Button 1

Just Follow this step :
Step 1.select LEFT IMAGE and set these constraint
Left
Top
Bottom
Snapshot show :
Step 2 : Select Right Image and set these constraint :
- Right
- Top
- Bottom
Snapshot here :
Step 3 :Select Left Image and CMD+ Drag mouse from left to right image and select SELECT HORIZONTAL SPACING
Snapshot here :
Step 4 : Select Left Image and CMD+ Drag mouse from left to right image and select EQUAL WIDTH as image shown in step 3.
Solved !
Hope it helps.

First "control" click & drag from one button to the other and set it like in IMAGE 1 below.
IMAGE1 - http://imgur.com/J3nmkIx
Set pins to the opposite corners and unclick "Constraint to Margins"
IMAGE2 - http://imgur.com/ciBtnLz

Add such constraints in IB:
equal widths
equal heights
pin top of one button to top of other
also for bottom
set left, top, width and height for left bottom

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

Align 3 UIButtons to the bottom centre of UIViewController

I have a UIViewController that consists of a UIImageView then I want to provide three options to user namely, rotate, crop, draw.
I want these three buttons to remain always centred at the bottom and maintain an exact distance with the UIImageView.
How can achieve this using auto layout I have done a lot of search as well as work but to no good use.
Set constrain like this ,
First set Constrain to centre button(Crop Button). like,
Crop button :- Bottom spacing from bottomlayout , height and width (According to your requirement), and Centre Horizontally in container (now your crop button is fix in centre)
Rotatebutton:- set Horizontal spacing from crop button , rotate buttons top=cropbuttons top , rotate button's bottom=crop buttons bottom , and Rotate button's height and width equal to Crop button's Height and Width.
(set constrain to draw button same as rotate button)
So, all button's look same .
check in image ,
1) Set constrain to Buttons
2) Now set constrain to imageview
3) Output
Edit :- Here i have attached demo ,
Demo for Centre button
(1) First add ImageView, constraints like this
(2) now add an UIButton in the bottom center named CROP. Give Constraints like this
(3) Now add 2 UIButtons named ROTATE and DRAW on either side of CROP
for ROTATE:
for DRAW:
(4) Finally select 3 buttons and give equalWith Constraint
No need to add Constraint to with ImageView. Add these buttons on a UIView and add the constraint on that view as Horizontally Center and also add ImageView on a UIView. Now add the constraintHorizontally Center on the parent view of Button to the parent of ImageView.
Your scenario should be like below,
left most button should have two constraints(cancel) : leading,bottton
right most button should have two constrains (yellow button) : trailing,bottom
Take one UIVIew and put thhree buttons in that view with equal distance from every side of that uiview.
now your constraints to all three buttons should like : leading,trailing,top and fixed height
And constraints of that uiview should be : horizontally center in container, bottom and fixed height
I am attaching screenshot aswell
You can check constraints from view hierarchy on left side.
I have set gray background color to just demonstrate this is view you not need to set color of that view.
By this way your three buttons will always display to bottom center of your VC
hope this will help :)
Hope the below steps might fix the buttons the way you wanted.Have not tried myself but you can try out if you have not tested these steps.
1.Pin those buttons to the bottom space container
2.Add a constraint from button to top space to image view
3.Play around horizontal spaces between buttons by pinning leading edge to the view for the left most button and trailing space to the view from right most button
4.Check center horizontally/vertically to the container

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 put buttons at the bottom of the label?

I follow Developing iOS 8 Apps with Swift course. The calculator is being developed there. As the first step, I add label (display) with the following constraints -
right - trailing space to Superview
left - leading space to Superview
top - top space to Top Layout Guide.Bottom with value 8
Then I should add the button (digit 7 in my case) and put it in the left top corner, but below the label. But it doesn't work - when I take and move the button (or all buttons as shown on the screenshot below), then the left blue line indicating left constraint appears, but there is no line indicating bottom constraint of the label.
What is wrong there? How to place buttons exactly below the label?
First of all in your case you should embed all the buttons in a single view and then add constrains in that view relative to superview (be aware that now buttons should have constrains relative to newly created view). To do that select all the buttons go to Editor->Embed In->View. The created view cant have top-left and below display constrains in same time because top and below are conflicted constrains. So next add the left constrain for the view and vertical space between display and view with following rule : Display.Bottom equal View.Top with a constant of your value. Finally your 7 button should be placed top left of its superivew

Resources