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.
Related
I'm attempting to put a generic view above and a tab bar underneath a scroll view, but can't figure out how to do it. Can someone explain how to properly place these constraints? Ideally I would like a solution non-programmatically, i.e. one that uses the storyboard. Thanks!
I've applied no constraints so far.
This is the desired output:
Well, In a UITabbarVC
Add UIView constraint top, leading and trailing to the self.view and assign height constraint to about 30% of self.view.height
Add UIScrollView add constraint bottom, leading, trailing to self.view and top constraint to bottom of the added UIView
First you have to give leading, trailing, top and a height constraint to your top view like so:
Then you need to give your scrollview, top, leading, trailing and bottom constraints like this:
And finally you give your tabbar leading, trailing and bottom constraints as shown:
First of all you need three things
UITabBarController
UIView
UIScrollView
Now, Firstly, you need to go in your .storyboard file and drag and drop a UITabBarController from the left hand panel
NOTE: If you are using XCode 10 , then that panel is renamed as Library which is available at the top bar with a Apple Home Button type button
Now, when you dragged UITabBarController, you will get some View Controllers attached with it by default.
Drag and drop a UIView to one of the view controller( preferably the first one) and set some Auto Layout constraints by selecting that View and clicking on the Add New Constraints button in the bottom bar ( where your Current zoom size is available) . You can add the following constraints :
Leading (aka left) = 0
Top = 0
Trailing (aka right) = 0
You also need to provide a height .
If you want to provide a constant height, then you can provide it using the same Add New Constraints option
However, if you want to provide a variable height like in Percentage , then hold Control on keyboard and click and drag from View that you dragged previously to the View Controller's View . Select the option Equals Height. Now it will take the height of the complete View Controller as it is taking 100% of the height of View Controller by default and we need to change that.
To change that, click on the View again , click Hide/Show inspectors (available on the top right corner) and select Size Inspector . There will be a constraint named Height which will be shown equal to superview . Click on the Edit button next to it and change the value of text field named Multiplier to the percent you want (make sure the value of constant text field is 0). If you want the height to be 20%, then input 0.2 and so on.
Next, You need to drag and drop a UIScrollView just below the custom view (that we just created and added the constraint to). Now set the constraints for the Scroll View in the same way you did for View.
For the Scroll View, you need to set the following constraints :
Leading (aka left) = 0
Top = 0
Trailing (aka right) = 0
Bottom = 0
and hit the Add Constraints button.
Now you will get the desired output . The only thing that will remain will be changing the colour of the View and changing the text label and icons for the UITabBarController items. You can change that via Storyboard easily.
Do let me know if you need any further assistance!
Am new to Auto layout constraints in Xcode. I have one Bottom View like UITabBar with 6 UIButtons. Without constraints I aligned those buttons with 5 space padding with each buttons and each buttons has 50 width.
Now,I am trying to make it by using Auto layout to support all Screen sizes.
In Storyboard constrains I set equal width for 6 buttons and I made 60
space between each buttons. I set first button 0 to leading space and
I set the last button 0 to tail constraints.
I want the buttons with equal width and flexible spaces between views in all device sizes.
Can anyone pleas help me? Looking for the help. Thanks in advance.
check this image and made your constraints like below...
RESULT:- preview in different sizes
With Stackview (For iOS 9.0 and above)
NOTE: If you have to make app for iOS 9 and later then UIStackView is another option for you
With iOS 9 and Xcode 7, you can now have the expected result with UIStackView and only 3 or 4 auto layout constraints.
The following step by step uses Storyboard.
Create 6 UIButtons and 5 UIViews. Align them horizontally. Give some color to your views.
Select all your views and buttons and click on the Stack button. You will have all your buttons and colored views aligned in a UIStackView.
Select your stack view, go to the Utilities panel and select the Attributes Inspector item. There, click on the Distribution button and select "Fill Equally".
For now, your stack view's height relies on your buttons intrinsic content size. If you're fine with it, you can go to step 5. However, if needed, you can give your stack view a height constraint. Select your stack view, click on the Pin button, check the Height button, add the required value and click on the Add 1 Constraint button.
As an alternative, If you want each element of your stack view to have its height matching its width, select the first button of your stack view, click on the Pin button, select Aspect Ratio and click on the Add 1 Constraint button.
You can check that your aspect ratio constraint is correct in your Document outline (left panel) and, if you need, you can change it with a different constraint in the Attribute inspector (right panel).
Now, it's time to give some external constraints to your stack view. Select your stack view. Click on the pin button, make sure the Constrain to margin button is not select and set leading, trailing and bottom constraints to zero. Make sure that the bottom constraint is related to your view controller's view. Then, change the Update Frames button to "All Frames in Container". You can now click on the Add 3 Constraints button.
Your stack view is now set.
Further remark:
If you don't need your colored views width to match your buttons width, you can build a stack view with only UIButtons and simply add spacing to your stack view in the Attribute inspector. However, you will have to find a way to add a background color behind your stack view. Apple states about it in the UIKit Framework Reference:
The UIStackView is a nonrendering subclass of UIView. It does not
provide any user interface of its own. Instead, it just manages the
position and size of its arranged views. As a result, some properties
(like backgroundColor) have no affect on the stack view.
I've build a Xcode project with 4 different stack views:
one with colored views and relying on the embedded UIButtons intrinsic content size for its height,
one with colored views and with a UIButton "equal width and height" constraint,
one with colored views and with its own height constraint,
one without colored views but with spacing and embedded inside a colored view.
You can find this project on this GitHub repo.
iOS 9 has a new UIKit class called UIStackView. It is very helpful in stacking up views horizontally or vertically, like the way you want. You should check out this tutorial: Raywenderlich: UIStackView Tutorial
Select all the buttons and in Xcode’s Pin menu
Create a horizontal constraint from the top left view to the top right view by selecting the red line to the selected view’s right side nearest neighbor
Create a horizontal constraint from the top right view to the top left view by selecting the red line to the selected view’s left side nearest neighbor
Create a vertical constraint from the top view by selecting the red line to the selected view’s top side nearest neighbor
Select checkbox near to Height, Equal Widths and click on "Add 22 Constraints"
Spacer views should no longer be necessary unless you need space between borders.
You can create a StackView with equal spacing which will dynamically adjust the space between your stack views to use up all available space without altering the size of your subviews.
Interface builder can be a bit misleading here. the value you put in for spacing is the minimum spacing required not what the actual spacing will be.
The StackView will handle your spacing, making the size of your buttons equal you can use regular constraints for.
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%).
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.
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