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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23645448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:55:35  来源:igfitidea点击:

How to programmatically define AutoLayout constraint to allow a view to be pinned to the edges of its superview

xcodeios7xcode5autolayoutnsautolayout

提问by SasukeIsCool

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

我刚刚开始尝试使用 AutoLayout,但不太明白。

Can anyone please show me how exactly to programmaticallypin 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)

任何人都可以告诉我如何以编程方式将视图固定到其超级视图,以便视图边缘和超级视图边缘之间的空间为 0。(本质上,我希望视图在横向时仍能覆盖屏幕)

Thanks

谢谢

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

最后,我是这样解决的:(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]];

回答by valbuev

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.

我认为您应该创建例如两个约束:值 = 0 的左边缘和值 = 0 的右边缘。您也可以创建左边缘和宽度 = 父宽度的约束。

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

回答by ERbittuu

You can use like

你可以使用像

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