【问题标题】:swift: Adjust UIView height based on constraints insideswift:根据内部约束调整 UIView 高度
【发布时间】:2014-11-21 04:19:42
【问题描述】:

我有一个 UIView 容器,它对超级视图有左/上/右约束。所以尺寸是动态的,除了高度。 它没有底部约束

这个视图里面有内容(标签等等)。这些元素也对彼此和超级视图有约束(以证明 em)

标签1

-> Label2.top : Label1.bottom (+superview的左右)

标签2

-> Label3.top : Label2.bottom (+superview的左右)

标签3

等等。

标签具有动态大小,因此,我无法确定标签的高度。它使用属性字符串。所以计算子视图rects不是解决方案

我想要实现的是考虑约束来设置主容器的适当高度 我厌倦了谷歌搜索,我得到的只是一些框架矩形手动计算,它不适用于约束。

这是内部元素的约束:

    self.view.addConstraint(NSLayoutConstraint(
        item:self.view, attribute:.Trailing,
        relatedBy:.Equal, toItem:label,
        attribute:.Trailing, multiplier:1, constant:0))

    // left
    self.view.addConstraint(NSLayoutConstraint(
        item: label, attribute:.Leading,
        relatedBy:.Equal, toItem:self.view,
        attribute:.Leading, multiplier:1, constant:0))

    // Top
    self.view.addConstraint(NSLayoutConstraint(
        item:parentView, attribute: positionConstraint,
        relatedBy:.Equal, toItem:label,
        attribute:.Top, multiplier:1, constant:0))

请帮帮我!

【问题讨论】:

    标签: ios swift uiview constraints height


    【解决方案1】:

    我刚刚在我正在构建的应用程序上遇到了同样的问题。我正在使用 Storyboard 编辑器,所以我不完全确定程序化组件,但是如果您在视图内的所有组件中添加垂直间距约束,那么您只需要在底部设置底部空间约束视图内的组件。

    http://cl.ly/image/2Y0y121s1e2V/download/Image%202014-11-19%20at%2013%3A59%3A15.png

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多