How to put buttons at the bottom of the label? - ios

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

Related

Align to the edge of the screen on iPhone X

How can I align a view to the top edge of the screen beyond the safe area?
Go ahead a select Top Space to Safe Area to create the top alignment constraint. Then select that constraint in the Storyboard or the Document Outline and examine it in the Attributes Inspector.
Click on the First Item which is Safe Area.Top and in the pop-up select Superview to align it to the Superview instead of to the Safe Area. Set the Constant to 0 and you're done. Your view is now aligned to the top of the screen.
Here it is running in the iPhone X simulator with the red view aligned to the top of the screen:
In the bottom right of that view there are three or four buttons that help you set up constraints. One of them shows you a popup where it lets you create constraints for leading, trailing, top and bottom. (I think it's the second from the right. Xcode is updating right now and I can't open it to verify)
By default it will create the constraints to the safe area but there's a down arrow you can click and there you can choose the view as the constraint target. If you also set the constant to 0 then your view will be aligned at the top to the view.

Xcode pin bottom constraint doesnt work

Why the pin constraint doesnt work? I have no constraints, and I want to pin one view to bottom of main view, so I select the child view, then I click the icon from "w:Any h:Any row" in Interface Builder bottom right (Pin), then i click the bottom "I" to create bottom constraint, but if I resize the controller view (in design mode) the view is not moved but something like this appears: (The red line with number - instead of moving the blue view to bottom).
I have pinned it to "bottom layout guide". I also tried to pin it to "view" from dropdown while pinning, but that doesnt work too.
If I look on the "Issue navigator" there is only warning saying "Position and size are ambigious for View"
Without any other other constraints there is no way for Xcode to know what you want to do with the view, all it knows is that you want the bottom of your subview to stay 99pts away from the bottom of the superview, but doesn't know what to do with the top, left, or right edges.
Try adding constraints for the remaining 3 edges, it can be as simple as setting the width and height (the options under the bottom edge in your screenshot), or something more complex depending on what you want to achieve.
You need to define the position of view properly. So that constraints can define position and size of the object.
Like in your case you have only provided constraint from the bottom, which will not be sufficient to define position. So you need to define either Height and Width constraints or top, left and right constraints.
And as long as long red line is being shown keep adding constraints.

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 equally space items in Xcode storyboard?

I want to have 15 buttons equally spaced between them and correctly leading and trailing the superview vertically. I set constraints so that the left set of buttons aligns to the left margin and the right set aligns to the right margin. The first button leads from the top margin in each set, and the remaining buttons each have a constraint for vertical space set to the upper neighbor. I thought this would be enough to make it work but it wasn't. As you can see, for an iPhone 4, the number of buttons displayed on the screen is only 12 so I would definitely need to somehow dynamically change the button size depending on the screen size. I am attaching a screenshot from the storyboard that shows the Any x Any editor view and a preview for the 3.5-inch screen.
This was really easy to accomplish using stack views. Simply select all the buttons and click the leftmost icon (bottom right side of left pane) which is named Stack, and see the magic happening!
Try to set the constraints like this:
First select the all elements on left side and give leading constraint:
Now select first element and give top constraint:
Now select the second element and select it , and drag it to first element and give vertical spacing: , repeat giving vertical spacing for all the elements, So that each element will be vertical space constrained with its top element!!.
Do the same for elements on right side.
Give the trailing constraints for all elements on left,
and the select top element and give top constraint
and then select one element below top element and give vertical spacing between those two, repeat it for all the elements!!! So that each element will be vertical space constrained with its top element!!
Note: I only gave left constraints, you can try for right side too!!!!!
Add height constrain to the top left button to be 1/15 of the superview height and for all buttons let their height equal to the top left one.
Note it will work only if the vertical spacing is zero between buttons.
If you have a vertical space value for example x, then set it as 1/15 - 14 * x
Note #2 in iOS 9 only, seems the stack view will help perfectly in that

How to add Equal Spacing and equal width for button in iOS Auto layout

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.

Resources