I have 4 buttons which are supposed to be at the bottom of my screen, right next to each other -- 0 pixels apart. I want it to work for any screen size, or at least for iPhone 5, 6, and 6+. I'm trying to add constraints, but it doesn't do what I want it to do.
It's my first time working with Swift and XCode, please give me a hint.
Thanks in advance!
The best practice is to use UIStackView
Just drop it to storyboard
Add vertical spacing constraint to bottom layout guide
Add horizontal spacing constraints to leading and trailing of the superview
Add height constraint
Now add the buttons as subviews of the stack view
set the stack views properties: Horizontal, distribution and alignment as you want
Good luck
Options are:
Set up constraints correctly
Use a StackView
Use a TabBar format
Use a ToolBar
Or, complex-ish calculations in code.
Select all of the buttons in interface builder so they're all highlighted and bring up the "Add New Constraints" popover. Add the left, right, and bottom constraints, and check "Equal Widths".
Related
XCode 9.3: macOS 10.13.4
I have an swift app where the buttons are off-kilter. I need them at the bottom of the screen and centered. The iOS simulator for iPhone 8 shows the buttons correctly. However, the simulator for the iPhone SE and the actual SE (my iPhone) show the buttons anchored to the right edge of the screen. How can I insure that the buttons are displayed correctly on all iPhones?
First sol
this layout can be done correctly if the center of the middle button is equal to the center of the screen
Second sol
create a UIStackView with these constraints
1- left , right , bottom to screen
2- height say 100
with distribution set to fillEqually , spacing set to say 20
drag the buttons inside it and they will look right in any device
Remove the constraints from the 3 buttons: Calc, Clear, and Help
Align clear to be horizontally centered
Align Calc trailing offset from Clear's leading.
Align Helps Leading offset from Clear's trailing.
Align Clear bottom offset from the bottom margin.
Align Clear, Calc, and Help along to center vertically with one another.
You have two choices: 1) use the legacy struts and springs mode in interface builder to distribute your items as you need them or 2) spend the time learning auto layout, and use it instead. There isn't any easy answer because of all the different screen sizes, and you haven't even considered iPad screens here. The best way to pick up auto layout is to view the WWDC videos on the subject. Yes, it will take longer, but your app will respond better to screen changes, and you will also be able to inherently accommodate new screen sizes.
Use a horizontal stack view for buttons, and set spacing between buttons.
Approach 1 - Set trailing space and leading space, pin the view to bottom and set a fixed height for stack view.
Approach 2 -
Set fixed height and width to stack view and center horizontally, and pin the stack view to bottom.
Always try to use stack view, with that you will reduce the number of autolayout issues you run into.
Please add the constraint for stack view and adjust like that:
I have a view with two button. I have added auto-layout. But bottom space is more in iPhone 8 plus than iPhone 4s.I want to keep the spacing ratio of top and bottom same in all iPhone. How to fix it ?
Updated
For Specific Ratio : To my understood, We can give Multiplier for Centre Vertically Constraints. Here ratio of top and bottom space will be same in all iPhone series. I gave Multiplier as 1.4
==================================
I know having several answers there. But, no one used UIStackView to add two simple buttons.
Drag two UIButton, select that two UIButtons, click Embed in Stack icon from here.
Stackview Properties
Give following things for UIStackView,
Set Axis as Horizontal, Distribution as Fill Equally, Spacing as 10 in Attributes Inspector
Stackview Constraints 1
Give constraints for that UIStackView, Check/Uncheck Margins, Left and Right = 0, Height as 30.
Stackview Constraints 2
Still, some constraints are not satisfied. Press Control and hold it, Click StackView and Drag to SuperView, you will get black pop up. Choose Center Vertically in container.
Stackview Constraints Customization
If you want to change height of UIButton or Bottom Space, click UIStackView and in Size Inspector, you can change height or move up/down.
If you are using Auto layout then set constraint like this way
set constraints to bottom and leave top constraints
1) Select both buttons
2) Click on "Resolve auto-layout issues" button on bottom and click on "Reset to selected Constraints"
You should have 2 stack views. One Horizontal that contains the 2 buttons, and another vertical that contains the previous stack view. Set the horizontal stack view to "fill" and "equal spacing" and the vertical stack view to "fill" and "equal centering." Below I've included a couple of pictures to illustrate what I mean.
THE ONLY CONSTRAINTS ARE ON THE STACK VIEWS THAT BIND THEM TO THE SIZE OF THE CONTAINER VIEW, OR PARENT STACK VIEW.
This allows you to truly create an auto layout that scales with the device's screen size.
Use Constraints like this. These buttons have bottom fix with superview.
I'm new to iOS programming and seems to be having a hard time working with constraints. I added a textfield to the view and tried to position it where I placed it on the view. I added few constraints using the "Add New Constraints" button on the bottom right of the storyboard but the textfield position is still off when I run it on a simulator. Can someone help me please?
Update:
I tried adding these constraints but still no luck
So, for some bizarre reason, it started working after I changed my "view as: " option from an iPad to iPhone. Its not a proper solution but I am just going to use it temporarily.
You need right click the button in your storyboard and drag the constraints line to the related component.Then edit it.
demo
You can also add constraints by code,like vfl or masonry.
Every view in auto layout need to calculate position(x,y) and size(height, width) so that we have to add constraints to that particular view
For example:
mytextField I would like to align it in top of view with 10px from top of view and left, right 10px, height is 100px
Give constraints like place textfiled in view and add Top constraint and set value 10, add leading constraint set 10, add thrilling constraint
set it 10, and finally, add Height constraint 100
Then the text field will align at top of view with the required frame on any device.
you are using 'Horizontal center and vertical center' constraint, when you run app on different device other then the device you are using for design, TexField will be on different place because vertical center and horizontal center will be different on that screen.
User top, bottom,trailing and leading constraint to achieve this.
I had inserted a ScrollView into UIViewController and dragged ScrollView to fill the space between the navigation bar and the RAM label below:
But when I run the app, the ScrollView does not fill the space:
Please help me! Thank you very much.
P/S: Sorry for my english is bad.
The scroll view is not covering up the whole thing because you are running the app on a much bigger phone. The simplest solution is to run the app on iPhone 5.
However, if you want to solve the problem on all sizes of iOS device, you need to add constraints.
Constraints are things that tells a view when and how much it should resize and where it should be positioned.
To add a constraint, just select the view you wish to add a constraint to and go to the bottom right corner. You will see 4 buttons:
The leftmost button is used to embedding views in stack views. This is a feature of iOS 9. If your deployment target is lower, just ignore it.
The second button to the left is for adding constraints related to alignment - where the edges of the views are, what its baseline is and where it is positioned in the X and Y axes:
The third button to the left is used to add constraint related to margins, width, height and how the width and height should change when it is asked to resize (keep the aspect ratio, for example):
The rightmost button is used to let Xcode decide what constraints you should add. And I think most of the times its choices are okay. Sometimes though, you still need to do some tweaking before it works.
"So... what constraints should I add?" you asked.
Well, I think I should teach you how to think when you want to add a constraint. This way, you can figure it out yourself in the future.
You should first let Xcode guess what constraints you want. Just click the rightmost button and click "Reset to Suggested Constraints". This can save a lot of work if Xcode can get it right. So remember to always do this first.
Then, run your app on various devices and see if the view's position, size, and alignment are as you expected. If it is not, you might have to add and/or remove some constraints.
For example, if you found that your view is always the same size on different devices, (that could be bad because it means that some content my go out of view on smaller devices) it's probably because Xcode added a width and/or height constraint to the view. You should delete that so that the view's width and/or height is not fixed.
You can find your view's constraints in the view hierarchy:
Just select the constraint and press delete.
Uncheck Adjust subview option and add
scrollview.view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
You need to add constraints for your scrollView. Set the leading and trailing constraints to 0. Pin the height of your scrollView and also don't forget to set the top layout constraint. You can either pin the height or add bottom layout constraint to your page control.
Constraints are very important and its even more important to set it correctly. Check the Apple Documentation - Working with constraints in IB
Uncheck constrain to margins and add 0 every one of the four limits of spacing to nearest neighbour.
My guess (from the little information we have) is that you are creating a constraint from your scrollview to the top of your view with a value equal to the height of the navigation bar. Set the value of this constraint to 0.
Just set the 4 constraints to 0 to the area you need and then uncheck the "Content Layout Guides" checkbox in the constraints tab here. It will automatically adjust to the area you have specified.
The checkbox to uncheck
I have two views, v1 and v2, that are horizontally aligned inside a UIView. I want the views to have equal width. So I select the pin menu in auto-layout and select the width constraint. And then I go to the "Resolve Auto Layout Issues" menu and click on "Update Frames", but doing that just make my views v1 and v2 disappear. Does anyone know how I might get my views to have equal width? For comparison, in android I would place the views in a horizontal linear layout and give them both a weight of 1.
One of the views needs to have a fixed width, or the views need to be pinned to the edges of the superview and to each other so that you have a chain of constraints running from the left to the right edge of the superview. Once you have one of these two conditions, you can select both views and choose "Equal Widths" in the pin menu.
Is that the only constraint you're setting? XCode probably needs more information to avoid an ambiguous layout. In addition to "equal widths" you probably also need to pin the edges to the superview, set a distance between the 2 views, and perhaps try setting a min width constraint so they don't disappear. http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1
I'm sure that you could also select the two views, and in the "Alignment Constraints" select both "Leading Edges" and "Trailing Edges" constraints and then manually set the width of one and the other should follow suit.
I think anyone who comes to this Question and thread of answers should read the following answer too!
It shows a simple way to do this.