ios- Autolayout- add constraint respect to navigation item - ios

I have just started to move my application to autolayout.
I want to give constraint to a label '0' pixel below navigation bar.
I tried but did not found solution.
Here are screen shots.
In storyboard I gave constraint from superview.
In IOS7 it works fine.
But in IOS6 it goes below navigation bar by 64 pixels, as superview starts below navigation bar.
If I could add top constraint for label below navigation bar it will solve the problem. But I cant find how to do it. I searched through few questions. but dint got the answer.
Please help.

Update for iOS 11:
iOS 11 introduces the concept of safe areas, and the burden of container all the content in a visible area is, again, at the hands of the view, rather than a tango of view and controller.
See Apple's documentation here.
When working with storyboards or XIBs, it's as simple as enabling the feature in Xcode. This is backward compatible and should work as expected under iOS 10 and below. In code, you would use if (#available(iOS 11.0, *)) in order to decide whether to set constraints against the safe area guide/anchors or use the legacy guides on older systems.
When using storyboards, you need to add a constraint to the top layout guide. This will take care of your issues, as on iOS6, that is 0pt and on iOS7, it is dynamic. If you are using a nib/xib file, it gets a little more complicated; you need to add the constraint in code. The easiest way to achieve this is to create a constraint with 0 vertical space to top, create an outlet to this constraint in the code and in viewDidLayoutSubviews, update the constant of the constraint outlet to the value of self.topLayoutGuide.length.

You just have to make some modifications in your constraints :
1) Give a height constraint to your label
2) Pin your label to leading view - Horizontal Space Constraint
3) Add a Vertical Space Constraint
3rd step has to be careful [I'm attaching an image for your reference.]
And after doing that, check if the red arrow mark still persist on your View Controller.

Related

Swift - UIButton is not pinning to the bottom of the screen

I am having trouble pinning the UIButton to the bottom of my UIViewController, (There is a tab bar if that helps).
Here are the constraints of the UIButton.
Originally, it all worked fine, but ever since I added iPad support to the application I have issues. on the iPad the UIButton is pinned to the bottom of the screen, directly above the tab bar. However on iPhone, there is this empty gap at the bottom.
What I Tried
Doing some research I saw that sometimes split view controller causes a grey bar, I added this line of code to resolve it and it fixed the bottom bar , but this constraint issues is still causing me a headache.
self.extendedLayoutIncludesOpaqueBars = true
iPad View Hierarchy
iPhone View Hierarchy
You have an option on Xcode to vary your constraint depending of the size of the screen.
You can create two constraints, one that works on iPad, one that works on "classic" iPhone.
Here, if I create a new constraint during the variation, I can then select on the right window which variation you want the constraint to be installed on.
There is a whole topic about that here
Remove current Constrain and Try unchecking "Constrain to margins" button.
Try setting a constraint to a View instead of a SafeArea
1- Give it a constant height for example 40
2- While setting constraints, uncheck (Constrain to margins)
3- Give it a proportional width constraint with the super view 0.8
Note1: If you don't want to give it a fixed height you can do the same step 3 but with height and not 0.8 but maybe 0.2 to less depending on your design.
Note2: You can use a scroll view, it will make it so easy for you

White space on bottom iPhone X Swift

I know that another question with same issue is there but the answers didn't solve my problem.
I have tried the solution here but still, I am getting the white space.
My bottom constraints are with SuperView, not bottom layout guide. I have tried expanding the safe area through code but still, nothing worked.
I am using another embedded view controller inside this view.
Update
View hierarchy
Remove "safe Area" in any storyboard ViewController. And then set constraints accordingly like this.
As you are providing target 8.0 support you can just remove safe area and provide simple constraint same as below image then your white space will remove for sure.

Swift - Why ScrollView not full screen?

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

iOS Swift, Constraint pushing view off sotryboard

I'm working on my first swift app and I found out quickly that I need to use constraints to properly layout my page. So far I'm running into two issue.
The first one is that after adding some constraints, my view is pushed off the screen on the storyboard, but it looks fine(outside of my second issue) in the Assistant editor.
My second issue is that on some phone sizes the constraints are causing the text fields to expand when viewed in the Assistant editor and on an iPhone 6s test device.
I've added some screen shots below. Any help with this issue would be really appreciated.
Each time you add a constraint in storyboard, it is not automatically applied to your working view. You may think it should be applied immediately, but try to think think like this. If you have a view, and you apply height constraint (for example) and if Xcode applied it automatically you would get a view of width 0, since you haven't applied width constraint. That's why you need to tell Xcode to update constraints explicitly.
So when you want to update constraints, select view you want to update and go to Resolve Auto Layout issues, and click on Update Frames. This is located in the bottom right corner of your storyboard. It looks like a small triangle between two lines. Here you can update all views or just selected views.
For the second issue, you need to apply fixed width constraint to your stack view, instead of adding leading and trailing constraints. By doing that stack view will have the same width on each device.

Autolayout - Why does "Vertical Spacing" sometimes attach to the opposite side of a view?

When I select "Vertical Spacing" in interface builder, I expect the bottom of the upper view and the top of the lower view to have a relationship. However, frequently what seems to happen is that the bottom of the lower view gets linked to the bottom of the upper view. Then at runtime the bottom view will have the wrong frame (after resizes, animations, etc.). Is there a way I can ensure that views are consistently linked bottom to top?
[EDIT]
Here's another example. I set a height of 419px. I select "Top space to top layout guide". But instead it puts a constraint for how far the bottom of the imageview should be from the top of the superview. Then I change the height from 419px to 374px, and the view has correct height, but a gap at the top.
I've noticed this many times (usually when attaching the top of something to the topLayoutGuide), and I've put it down to a bug in Interface Builder. It seems like the issue has gone away in the betas of Xcode 5.1
The only workaround I've found is to initially move your view away from the view you're attaching it to, and then create the constraint. In this situation, Xcode will usually do the right thing. Then manually edit the constraint's constant back to 0 (or whatever you want it to be).
Instead of adding vertical spacing, try by adding "Top spacing to nearest neighbor" constraint. Please refer attached image,
Hope this will help.
This issue is fixed in the latest versions of Xcode. You can now select which part of the constraint you want to attach to. It may attach wrong initially, but the constraint editing controls are much better now.

Resources