How to I align 4 buttons together? (autolayout) - ios

I'm trying to align 4 buttons together so they look like this:
I want it so that ONE has a 0 margin to the left, right and bottom, TWO has a 0 margin to the left, right, and bottom, THREE has a margin to the top, left, and right, and FOUR has a 0 margin to the top, left, and right
In order to support iOS 8, I do not want to use stack views

You can add 0 constraint to all their sides, then choose all at same time and give them equal width and height, if custom height then just need to set height constraint to 1 of them, then you can achieve the design

You can set proportion width for all four buttons. Set width = Superview.width and then Set multiplier =0.5 This will make sure that your button width is half of buttons superview so will fit two button in it.
To do so, Right click on Button, Drag to super view and select EqualWidth constraint. then go to constraint and edit multiplier of recently added width constraint to 0.5 (or 1:2).
You can check this video for detail

Use Ctrl+click from one button to other button and choose equal width.All the buttons will have equal width in this way.

Related

How do I fix layout issues with constraints in Xcode?

I used the following constraints to build one screen for my app. However the icon buttons get messed up in different screen sizes (screenshots below). Is there a way to rectify this?
1- Give equal heights and equal widths to all 4 boxes.
2 - For first one-> leading, top,
- For second one(travel and place)-> horizontal spacing to 1st one,leading and top
- For third one(activity)-> vertical spacing with 1st one ,horizontal to utilities and leading
- For fourth one-> trailing , vertical spacing to 2nd one and horizontal spacing to activity
What I would do is:
Set a proportional size (width and height) to the first button, maybe 40% of the container.
Then for the other 3 buttons, you can set them to the same size as the first.
Add some space between them.
And thats it.
1 - Take two views as two rows
2 - Let for first view (row), add two subviews , give the width of first equal to width of superview and multiplier 0.5 , let the second view align leading to first view trailing and width equal to first view
3 - add the button as subview in each view with top , right , bottom and left spacing as desired. Repeat the same for 2nd button in 2nd view.
4 - Repeat the above steps for row 2 view
hope this helps
These are actually pretty straightforward when you think about how all the views are supposed to relate to each other. I made a demo storyboard to show you how I'd wire it up. The key things:
Each box has a 1:1 aspect ratio.
Boxes in a row have equal widths to each other
Each box is constrained on both sides to the edge of the view or each other
The icon's height is set to a % of the box's height
The labels are pinned to the top, right, and left of the box
Obviously you'll want to adjust your styling as appropriate, but that should get you the effect you want.

iOS Auto layout in percentage

I wanna create a simple iOS application using autolayout that displays three UIViews. One on the top, one on the left and
the other on the right. The top view should be 50 pixels high and fills the horizontal space completely. The left column should fill 40% of the horizontal space and should fill 100% of the
vertical space. The right column should be 60% of the horizontal space and 100% of the vertical space. Finally in the left UIView wanna add a single button.
Yes, You can.
Add first view at the top, add four constraint, top 0 left 0 right 0 and height 50.
Add second view at bottom of first view, make it top 0 left 0 heigth 100.
Here is the trick, choose first and secound view, then make them equal width.
Choose secound view's width constraint, and change the multiplier to 0.4.
The two existing answers didn't work for me. My answer was built using Xcode version 10.1 (10B61).
The core question is "How Can I Set a View’s Width to be a Percentage in a Storyboard". Answer: the view's width needs to be associated with the superview.
Explanation:
In the Main.storyboard (already containing a View Controller and a View), add a View. I’m going to call it YellowView). And change its containing view name to SuperView.
In the YellowView's Attributes Inspector change the Background to Yellow.
Click on New Constraints and Top = 0, Leading (left) = 0, height = 50
Control Drag from the YellowView to the Superview and let go, in the resulting dialog select "Equal Widths"
Note: YellowView's width becomes the width of the SuperView.
In the Storyboard, Double-click the width constraint we just added and change the Multiplier to 0.25 (Note: Typing .25 will not work - the leading zero is necessary)
Your constraints should be like,
first view - top,leading(left),trailing(right) and fix height
left view - top, left(leading), bottom and equal width with superview with multiplier 0.4 (or width constraint with 0.4 multiplier)
right view - top,right(trailing),bottom and equal width with superview with multiplier 0.6 (or width constraint with 0.6 multiplier)
hope this will help :)

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.

how to design auto fit for all iPhone devices like 4,5,6 and 6+ using storyboard auto layout

I need help to design the layout for 3 rows and 2 columns of uibuttons in a single view controller on storyboard. Its fit for all iPhone devices like 4s, 5,6,and 6 plus. I don't know how to use the constraints. Please guide me to do this. Here is the sample image i want like this
I have setup a github repo regarding your problem link
Steps:
1) Top Bar height = 0.17 * superview.width
2) Bottom Bar height = Top Bar height
3) MidSection covers all area between top slab and bottom slab
4) Row1 height = MidSection height * 0.33
5) Cell1 width = Row1 width * 0.5
These are the major constraints, hope it helps
Follow these steps.
Fix the height of the Top View(In your case the green view). Give the Leading, Trailing and Top constraints to the Superview as 0.
Fix the height of the Bottom View(gray one). Give the Leading, Trailing and Bottom constraints as 0.
Now place a view in the middle of the top and bottom view. Give Leading, Trailing, Bottom and Top constraints as 0.
Place button1 in the middle view. Give the Leading and Top constraints 0 and width proportional to middle view in such a way that the button should be half of the middle view.
Give the height of button1 proportional to middle view with the multiplier 0.3. (Change the multiplier value if needed)
Make a duplicate button of button1. Give the height and width equal to button1. But the Trailing space to middle view as 0. And Top constraint too.
Do the same thing for the remaining four buttons, but give Leading space or Trailing space as per the requirement.
Here the important thing is the height of the button1. You need to check for each value in all the devices. Or else you can simple use a scrollview and fix the content height of it.
Check out the image if needed.
Hope this helps you.
I made a sample project for this. You may take some idea from it;
http://jmp.sh/Xli1hQ1
This guide should help you.
Autolayout guide

No fixed height in interface builder, constraint predicament

The screen I'm trying to create is very simple. I have two UIViews stacked atop one another. The top UIView, topView, has height of 40px, and the UIView below, botView, takes up the rest of the screen. However, sometimes topView should not appear and I want botView to take up the entirety of the screen.
What I've tried is setting width, leading/trailing space on both views. Then topView gets distance to top layout guide, botView gets distance to bottom layout guide, and then an additional constraint for vertical spacing between topView and botView. But this results in xcode yelling at me that I haven't set a height or y constraint on one of the views. Of course, I can't set a height constraint because I do not want either of them to be fixed height.
I'm sure there must be an elegant constraint solution here that I just can't seem to figure out. Thoughts?
If topView should always be 40px height, then add a constraint for the height of topView.
As for botView, you can keep the current constraint that says its top has to be correlated with the bottom of topView. But add another constraint for bottom view with a priority lower than required that says it should be 0px away from the top of its superview. This way, when you remove topView from the containing view, botView's constraint related to topView will be deleted, and therefore will satisfy the lower priority constraint.
And if you want to push back topView in, recreate the botView to topView distant constraint with required priority.
There are a couple of ways to do this. One option
In IB lay out your views. Then pin top view 0 px from the top, bottom, left, and right. Specify a height constraint of 40 px as well. To easily do this click on your view, then in the lower right hand corner click on the button that looks like | + |. Click on the dotted red lines to specify the constraints. See image:
Do the same for the other view. Don't specify a height for this view because it's height is dependent on the 40 px view for its height. Next find the height constraint for the 40 px view. You can can use the assistant editor to control drag an outlet to you view controller just like you would with a UIButton or any other IB element. When you want to hide your 40 px view set the variables constant property to 0. When you want to show it again set it back to 40.
self.topViewHeightConstraint.constant = 0;
The other option you could go with is to pin the top view 0 px to the top, left and right. Don't provide a bottom constraint but specify the height as 40 px. For the bottom view add add a left, right, and bottom constraint of 0 px. For the top constraint add a constraint of 40 px from the top superview. Again add an outlet for the most recent top constraint and when you want the view to be larger set that constraint to be 0.
An alternative: In IB, add a distance to top layout guide constraint for BOTVIEW (it will be 40...) Then click-drag this constant to the VC to create a NSLayoutConstraint property.see also here
Now, upon the condition that hides topView, you set self.myConstraint.constant = 0; This will force botView to resize to meet the condition of being 0 from the top.
You may have to clear your current constraints to get rid of errors, but this should give the desired behavior.
As for "topView" like Tom Ahh suggests, go ahead and give it a height constraint of 40 and pin it to the top as you're already doing.

Resources