I am having some major issues with my constraints, all I did was select add missing constraints and it looks fine in my storyboard and on an iPad like so,
But when I run it on iPhone, it looks like this:
What do I need to add to fix this issue...this is what I got so far:
Don't relay only on Add missing constraints , you have to make your edits after applying them
I see some constraints with leading
949,344 and 283+
and this may exceed width of some iphone screens
Simply remove any leading and trailing , and centerX the items with width constraints
also using UIStackView is good for the lower textfields
The easiest way to solve your problem with the constrains in my opinion for this type of layout is to embed your views in to Stack View. With Stack View you can easy center this views in to Screen by adding some costrains to the Stack View. With Stack Views if you need some adjustment you can do it only with few mouse clicks.
Select all your views
Click embed in the Stack View
Delete constrians that you don't need
Adjust alignment, distribution and spacing
Add trailing, leading and heigh constrian
Now it should looks good.
Related
I have a UILabel and a Switch on storyboard, I use Stack View to group them, however,after embedded into a horizontal Stack View, I am not able to resize the stack view but only can move it around. Why?
(I am using XCode 8 + Swift 3)
===== UPDATE ======
Thanks guys suggesting for adding constraints, but .... I added constraints, but I still would like to see the constrainted layout on storyboard instead of messed up layout.
E.g. I added constraint to leading and trailing space to be 8 pixel away from parent, but I see red line which xcode gives to indicate the actual layout is not showing in the constraint way. That's the problem, I can only drag the stack view, but I want to resize it to match my constraint so that it looks same as that in real device.
You need to add constraints to set the the placement of the StackView. To set the distance between the two controls, simply set the spacing as I've done in the image below (Spacing = 50):
You can add the width constraint to change the width.
In Interface Builder, start by dragging out a vertical stack view, and add the flowers label, image view, and edit button. Then set up the constraints as shown.
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 in a view and i have a requirement to increase the distance between those views in aspect ratio to the main view. I am using auto layout.
Thanks in advance.
One way to solve your problem is -
For your upper view give top space constraint with super view and other required constraint.
For your lower view give bottom space constraint with super view and other required constraints.
You don't really need to give vertical space because your views will be pinned with fixed space with top and bottom of super view, so if screen size will grow space between your views will automatically grow.
Important to note however: You must add the height constraint for both views and have either fixed width constraint or have Leading and trailing space constraints.
See ScreenShot
Since you have more than one view, I recommend doing the following assuming this is the result you are looking for:
STEP 1 :
select all your views
Step 2 :
go to Editor -> Embed In -> Stack View
(now it will group them together) I'm assuming that you set the width and height constraint for each of your views before doing this.
Step 3 :
once you have done that select the stack (not the individual views). but rather the entire stack (I would do this from the side menu) and set the top layout and the bottom layout.
step 4 :
Go here and make these settings for the stack as you have it selected
and that should do it.
Let me know if I should clarify anything.
UPDATE:
If you do not want equal spacing, you can still use the above method by playing around with different stacks, etc.. However, There is a method I've used in the past to get this, which is setting a multiplier instead of a number for say spacing between bottom layout and view. Here is an example that might help below:
you can play around with it to get your desired results.
I want to insert 4 Subviews with exact 25% percent width of the SuperView.
So, my question is how to achieve that with XCode's Autolayout? In Android I would use a GridLayout or a LinearLayout...
Thanks in advance
Just set your constraints like this image,you will get what you want
And this is preview in different screen sizes
You have to select all the four views, and add top, bottom, left, right constraints to zero and equal width constraint.
Your constraints should look like this.
In the screenshot, the four views are arranged over a container view. If you dont't want the container view, then you have to specify the height constraint also.
You should use UIStackView that is introduce in ios9 and xcode 7.0
Here is a link of the tutorial have a look its like a linear layout in android
http://code.tutsplus.com/tutorials/ios-9-getting-started-with-uistackview--cms-24193
There are alot other ways as this is the new one and it help to reduce work and time both so you should use it.Thanks
I have been struggling for days with this implementation, and even though I have tried to do every tutorial I found on the web, I still cannot make things work the way I want.
Basically, I am trying to put my login form in a scrollview, so that it takes the whole screen at first (and on all iPhones / iPads), and if the keyboard appears everything should move. The problem IS, my view doesn't take the whole screen... Either it is too large, or too high, even though in Interface Builder everything seams correct (from layout to constraints). Below and image of the layout I want to achieve (I am using an universal storyboard, with Size Classes and Autolayout enabled):
http://img4.hostingpics.net/pics/829115app.png
Can someone point me out on achieving this layout ?
Thanks in advance.
I would suggest pinning top, leading and trailing spaces of your scroll view to its superview. And set a bottom space constraint less or equal to the keyboard's height if you set it to 0, the scroll view won't be able to resize.
With your form layout set vertical center constraints and top space to superview constraints for your top label being more or equal than the distance you set in the IB, and then you can set relative space constraints between each of the components.
Hope I answered your question.
Edit: Just the provided project and got it working. I think the problem is caused by it being a containerView inside a scrollView. And both the container and the scrollViews content view adapt to the size of its subviews. Because of that, setting relative constraints won't help.
What I did was to set an explicit size (screen's size) to the containerView and setting setTranslatesAutoresizingMaskIntoConstraints(true) to it.
I modified your project and uploaded it here