Why does Leading/Trailing space show as -16? - ios

As I updated my Xcode from version 5 to 6 I saw a strange things in the storyboard. Here is one example:
What my requirement was - I wanted no space between my view and the container view.
What I did - I dragged the views left/right edge so that it meets the container margin. And then I put a horizontal spacing constraint.
Earlier the value used to be -
Leading/Trailing Space to:Superview Equals:0
Now after updating to Xcode 6 it always shows -
Leading/Trailing Space to:Superview Equals:-16.000000
Why does this happen?

That's container's margin. I think it applies the default margin to the edges. If you want to set up the Leading/Trailing Space to Superview constraint then you can do so.
If you have already created the constraint then you can select that constraint and uncheck Relative to margin
Please note the Relative to margin feature is only available on iOS >= 8, the app will crash (not sure but it should) on iOS < 8.

Try to uncheck "Relative to margin"

Related

How to scale up apps in XCode for diffrent screen sizes

I'm working on an app for iPad but when I set the contrains(so it works on iPad Pro/iPad Mini) everything moved. I don't know if I set anything wrong or if it's problems with XCode However, are there a way to just like scale up/down the screen without contrains? Or how to fix the constrains problem.
(I choose all buttons and click add missing constrains)
Picture:
IMO selecting add missing constraints is not exactly what you want. If you want your view to scale up when screen is larger, you need to set the constraints to its superview. So you just say I want it to be 10 pt from the leading edge and 15 pt from the trailing edge. By adding missing constraints Xcode probably tells the view to be xy points wide.
To set the constraints as I mentioned you need to Ctrl drag from the view to its superview and add leading constraint. Similarly for the trailing and eventually top and bottom constraints.

ios Auto Layout Constraint Issue

After upgrade my xcode to 7 I am getting the issue in autolayout. I have put a container in view controller for which I set the leading and the equals width to parent view (it means that I need container on full width of screen) every thing has working fine but the issue is with the leading constraint of that container. I have to set the leading constraint constant to -20 then it would be looks perfect on 6plus, but for all other devices -16 would do the same result.
The most easiest way is to resolve your issue is by going to your Storyboard scene or nib file click on topmost view in hierarchy and finally click on Resolve auto layout Issues button from the four option available at bottom right corner and update frames.
Xcode will take care of rest.
This is happening because from Xcode 7 iOS 9, Apple wants us to leave leading or trailing space at-least by 20 points for UI perspective.
I think it would be easier to add spacing constraints from your container to the super view. If you define Leading Space, Trailing Space, Top Space, and Bottom Space as "0" without margin the container should have the exact size of your device regardless of the iOs version and device type.

iOS autolayout changes leading and trailing space

I have some issues with auto layout, hopefully some of you can help me =).
I add a UIView to an empty view controller, like this:
Then I add constraints for leading and trailing space, like this:
Which results in this:
I can't understand why it sets them as equal to -16.0. If I change those values to 0, then Xcode complains about the width of the gray UIView.
When I run the app on iPhone 5 simulator, it looks fine:
However, when I run the app on iPhone 6 Plus simulator it adds white space on the left and on the right:
I don't want the gray UIView to have any space from the left of from the right no matter how big the screen is. How can I fix it? Thanks!
Double click the constraint and open it. There select first or second item and deselect "Relative to margin". Then set your constant as 0.
Xcode is showing -16 because it is calculating space with respect to a margin and not the edges.
Check following link for more info on margins. Editing Auto Layout Constraints
You can clear all existing constraints on the view,and in Any*Any mode, add constraints for all 4 space to superview, which are Leading, Trailing, Top, Bottom. Then it will have the equal width with the screen no matter how big the screen is.

Unexpected result for autolayout on iphone 6 plus

I've added a uiview on a view controller(VC), trying to set it's width is equals to VC's view's width.
What I set and expected:
And what i got on iphone 6 plus:
I've tried to change the constants of leading space and trailing space and no help.
Is there any special for iphone 6+ autolayout?
The view setting here is "Any width x Regular height"
-------Added Dec 2016
To people who is confused by this case like me:
There is a layout margin property in UIView, which is an UIEdgeInsets(8,8,8,8). It is something like css padding.
When the edge of your view is close to the edge of the superview and the preservesSuperviewLayoutMargins property is true, the actual layout margins may be increased to prevent content from overlapping the superview’s margins.
The default margins are eight points on each side.
If the view is a view controller’s root view, the system sets and
manages the margins. The top and bottom margins are set to zero
points. The side margins vary depending on the current size class, but
can be either 16 or 20 points. You cannot change these margins.
Try to remove the check Constrain to margins when adding constraints. Here take a look:
Constrain to margins is checked and its result
Now, after removing the constraints and re-adding the constraints with Constrain to margins unchecked, here is the result:
Sharon's answer works wonderfully, but you don't have to delete the existing constraints to solve the problem. You may edit existing constraints removing the extraneous margin:
Editing existing constraints to remove the extraneous margin will not work. You have to delete the existing constraints and add new ones keeping constraints to margin unselected.
I faced same issue for my "Done" button. The issue was my View (Done Button) was in base View hierarchy so i bring view back in root view (view controller's view). and set constraints accordingly.
My case set constraints Pin-> Uncheck Constraints to Margin -> Bottom,Leading,Trailing (Constraints value=0)
Make sure view hierarchy should not be following way.(Subview might be but cut view must not).
I had this bug in the iPhone 6 plus simulator. But in the 6 plus device this bug didn't happen. This could be just a bug with 6 plus simulators.

Xcode 6 beta 7: storyboard adds extra space on right and left sides

When I add subview to root ViewController's view, and with auto layout setup leadingSpace,trailingSpace,topSpace and bottomSpace to zero, there are appear some extra spaces on left and right sides (so if I print subview's frame its origin will be 16 and size less on 32 than should be).
So actually we get that leading and trailing spaces are not zeros...
As you can see on picture leading space - zero, but origin.x = 16
Earlier I wasn't working hard with auto layout, so my question is:
Is it a bug of new Xcode or a feature?
P.S. All frames and constraints updated.
iOS 8 adds the concept of “content margins” to UIView. The “What's New in Cocoa Touch” video from WWDC 2014 discusses content margins (under the title “Adaptive Margins”) starting at about 12m30s.
The default margins are 8 points on each side. Each end of a layout constraint can be relative to a view's margin instead of to the view's true edge (use File > Open Quickly to go to the definition of NSLayoutAttribute to see the possibilities).
Your constraint is “relative to margin”. When you create the constraint with the “Add New Constraints” popover, you can choose whether it's margin-relative or not:
It always defaults to margin-relative; you have to turn the checkbox off every time you add constraints if you don't want them to be margin-relative.
You can't change whether a constraint is margin-relative in the quick-edit popover of your screen shot. Instead, double-click the constraint to bring up its full Attributes inspector. There, you can use the popup menus to select, for each end of the constraint, whether it's margin-relative or not:

Resources