Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've just started trying my hands on AutoLayout and don't quite get it.

Can anyone please show me how exactly to programmatically pin a view to its superview such that the space between the edges of the view and the edges of the superview is 0. (Essentially, I want the view to still cover the screen when in landscape)

Thanks

in the end, this was how I solved it: (vc.view is the child view and self.view is the parent)

    /* pin Left of child to left of parent */
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:vc.view
                                                          attribute:NSLayoutAttributeLeft
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeLeft
                                                         multiplier:1.0
                                                           constant:0]];

    /* pin Right of child to right of parent */
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:vc.view
                                                          attribute:NSLayoutAttributeRight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeRight
                                                         multiplier:1.0
                                                           constant:0]];

    /* pin top of child to bottom of nav bar(or status bar if no nav bar) */
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:vc.view
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.topLayoutGuide
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant:0]];

    /* pin Top of nav bar to bottom of child view */
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.bottomLayoutGuide
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:vc.view
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant:0]];
share|improve this question
    
I think this should help. stackoverflow.com/questions/8156799/… –  mithlesh jha May 14 at 4:35
    
Thanks, this helped –  SasukeIsCool May 14 at 5:15

1 Answer 1

I think you should create forexample two constraints: to the left edge with value = 0 and right edge with value = 0. Also you can create constrains to the left edge and width = parent width.

[NSLayoutConstraint constraintWithItem:child_view attribute:NSLayoutAttributeWidth
                  relatedBy:NSLayoutRelationEqual toItem:parent_view
                  attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0];
share|improve this answer
    
Do you think it would work by adding 2 constraints to the sub_view for width and height(instead of left edge) and doing the whole "relatedBy:NSLayoutRelationEqual multiplier:1.0" thing for both constraints? –  SasukeIsCool May 14 at 4:23
    
Actually please ignore my first comment, how would you specify a constraint for an edge? –  SasukeIsCool May 14 at 4:29
    
My solution = your solution, but i offered to set left = left, top = top, width = width, height = height. –  valbuev May 14 at 6:18
    
Oh my bad. I thought you meant to add just 2, just left and width –  SasukeIsCool May 14 at 6:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.