【问题标题】:How to programmatically define AutoLayout constraint to allow a view to be pinned to the edges of its superview如何以编程方式定义 AutoLayout 约束以允许将视图固定到其父视图的边缘
【发布时间】:2014-07-01 23:09:53
【问题描述】:

我刚刚开始尝试使用 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]];

【问题讨论】:

标签: xcode ios7 xcode5 autolayout


【解决方案1】:

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

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

【讨论】:

  • 你认为通过向 sub_view 添加 2 个宽度和高度约束(而不是左边缘)并为这两个约束执行整个“relatedBy:NSLayoutRelationEqual multiplier:1.0”事情会起作用吗?跨度>
  • 其实请忽略我的第一条评论,你会如何为边缘指定约束?
  • 我的解决方案 = 你的解决方案,但我提出设置 left = left,top = top,width = width,height = height。
  • 哦,我的错。我以为你的意思是只添加 2 个,只添加 left 和 width
【解决方案2】:

你可以像这样使用

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多