如何以编程方式定义AutoLayout约束以允许将视图固定到其超级视图的边缘

时间:2014-05-14 04:05:57

标签: xcode ios7 xcode5 autolayout nsautolayout

我刚刚开始尝试使用AutoLayout并且没有完全接受它。

任何人都可以告诉我如何以编程方式将视图固定到其超视图,以便视图边缘和超视图边缘之间的空间为0.(基本上,我想要视图在风景中仍然覆盖屏幕

由于

最后,我就是这样解决的:(vc.view是子视图,self.view是父视图)

    /* 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]];

2 个答案:

答案 0 :(得分:0)

我认为你应该创建两个约束:对于左边缘,值为0,右边缘为值= 0.此外,你可以创建左边缘和宽度=父宽度的约束。

[NSLayoutConstraint constraintWithItem:child_view attribute:NSLayoutAttributeWidth
                  relatedBy:NSLayoutRelationEqual toItem:parent_view
                  attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.0];

答案 1 :(得分:0)

您可以使用

[view.superview addConstraint:[NSLayoutConstraint constraintWithItem:view.view
                                                      attribute:NSLayoutAttributeLeading
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:view.superview
                                                      attribute:NSLayoutAttributeLeading
                                                     multiplier:1.0
                                                       constant:0]];