0
votes

I'm trying to add a view inside a scrollView with constraint. Everything work well, but I don't know why, one of my upper constraint is not doing what I want.

My white square should be at the top of my red superview, and my superview is inside a UIScrollView. But there is a magic gap of 8 points between my red superview and the white square on IOS7... That's not what was into my storyBoard.

enter image description here

And what i put into inside my code. Self is a UIscrollview.

 viewFromController.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview: viewFromController];

NSArray* constraint_2 = [NSLayoutConstraint constraintsWithVisualFormat: @"|[n(200)]|"
                                                                options: 0
                                                                metrics: nil
                                                                  views: @{ @"n" : viewFromController}];
NSArray* constraint_3 = [NSLayoutConstraint constraintsWithVisualFormat: @"V:|[n(==l)]|"
                                                                options: 0
                                                                metrics: nil
                                                                  views: @{ @"n" : viewFromController, @"l" : self}];
[self addConstraints: constraint_2];
[self addConstraints: constraint_3];

It works on IOS8, but not on IOS7.

IOS8 >

enter image description here

constraint (
        "<NSLayoutConstraint:0x7fa1507351d0 UIView:0x7fa150734a60.trailingMargin == UIView:0x7fa150734b30.trailing + 100>",
        "<NSLayoutConstraint:0x7fa150735220 UIView:0x7fa150734b30.top == UIView:0x7fa150734a60.topMargin>",
        "<_UILayoutSupportConstraint:0x7fa150730380 V:[_UILayoutGuide:0x7fa150734c60(0)]>",
        "<_UILayoutSupportConstraint:0x7fa150732380 V:|-(0)-[_UILayoutGuide:0x7fa150734c60]   (Names: '|':UIView:0x7fa150734a60 )>",
        "<_UILayoutSupportConstraint:0x7fa150734200 V:[_UILayoutGuide:0x7fa150734d90(0)]>",
        "<_UILayoutSupportConstraint:0x7fa150735110 _UILayoutGuide:0x7fa150734d90.bottom == UIView:0x7fa150734a60.bottom>"
    )

IOS7 >

enter image description here

constraint (
    "<NSLayoutConstraint:0x15d49890 H:[UIView:0x15d468c0]-(108)-|   (Names: '|':UIView:0x15d44690 )>",
    "<NSLayoutConstraint:0x15d4c460 V:|-(8)-[UIView:0x15d468c0]   (Names: '|':UIView:0x15d44690 )>",
    "<_UILayoutSupportConstraint:0x15d46740 V:[_UILayoutGuide:0x15d57a20(0)]>",
    "<_UILayoutSupportConstraint:0x15d4f1b0 V:|-(0)-[_UILayoutGuide:0x15d57a20]   (Names: '|':UIView:0x15d44690 )>",
    "<_UILayoutSupportConstraint:0x15d54120 V:[_UILayoutGuide:0x15d4f780(0)]>",
    "<_UILayoutSupportConstraint:0x15d46be0 _UILayoutGuide:0x15d4f780.bottom == UIView:0x15d44690.bottom>"
)

Can someone could explain why there is such things happening (the difference is to the upper space of the whiteSquare of exactly 8 points)? (the 0 constant should not be changed, after all)

thanks.

1

1 Answers

2
votes

In iOS8 constraints are pinned to the UIView's layoutMargins of the view (8pt margin by default!), not to the actual superview's frame.

layoutMargins takes a UIEdgeInsets value that lets you explicitly define the whitespace that your views can use to guide where portions of the interface should be placed. source

To fix it, constrain your view to the superview's top directly as opposed to relative to margins. This can be done in IB. To do it in code, you could set the superview's layoutMargins to UIEdgeInsetsZero in iOS8.