【问题标题】:What constraints would prevent my UIViews from stacking on top of each other?什么约束会阻止我的 UIViews 堆叠在一起?
【发布时间】:2016-07-26 01:44:59
【问题描述】:

我的 UIView 像这样堆叠在一起:

每个 UIView 都在 wordContainer 内。每个 UIView 里面都有两个UILabel

我用两个子 UILabel 像这样创建每个单独的 UIView:

    var labels = [UIView]()
    for word in words {

        let wordLabel               = UILabel(frame: CGRectZero)
        wordLabel.text              = word.valueForKey("sanskrit") as? String
        wordLabel.numberOfLines     = 0
        wordLabel.sizeToFit()
        wordLabel.translatesAutoresizingMaskIntoConstraints = false

        let englishWordLabel           = UILabel(frame: CGRectMake(wordLabel.frame.width + 10,0,0,0))
        englishWordLabel.text          = word.valueForKey("english") as? String)
        englishWordLabel.numberOfLines = 0
        englishWordLabel.sizeToFit()
        englishWordLabel.lineBreakMode     = .ByWordWrapping
        englishWordLabel.translatesAutoresizingMaskIntoConstraints = false

        let wordView = UIView(frame: CGRectMake(0,0,self.view.frame.width,englishWordLabel.frame.height))
        wordView.translatesAutoresizingMaskIntoConstraints = false
        wordView.addSubview(wordLabel)
        wordView.addSubview(englishWordLabel)

        wordView.sizeToFit()

        labels.append(wordView)
    }

然后我把这个 UIView 的集合添加到我的wordContainer。这是我想的部分,我应该设置 NSLayoutConstraints。这个想法是我希望它们都正确地堆叠在一起。例如,它应该如下所示:

word: definition
word: definition
word: definition
word: definition

但它看起来像这样:

我将它们添加到 wordContainer 中,如下所示:

    let wordContainer  = UIView(frame: CGRectZero)

    var currentY:CGFloat      = 0.0
    let margin_height:CGFloat = 4.0
    var height:CGFloat        = 0

    for view in wordViews {
        var rect = view.frame
        rect.origin.y = currentY
        view.frame = rect

        currentY += view.frame.height
        height += view.frame.height

        wordContainer.addSubview(view)

    }
    let containRect = CGRectMake(0,0,self.view.frame.width, height)
    wordContainer.frame = containRect

然后我为wordContainer 设置了我的约束条件,这实际上允许所有单词存在的空间,如果它们没有堆叠在一起的话:

   wordContainer.bottomAnchor.constraintEqualToAnchor(cell.contentView.bottomAnchor).active     = true
   wordContainer.topAnchor.constraintEqualToAnchor(cell.contentView.topAnchor).active           = true

我的最新想法是,也许我应该为此添加约束。我已经尝试将NSLayoutConstraints 添加到cell 的高度,并且当我这样做时,所有单词都不再适当地布置。相反,它们堆叠在一起。但这似乎是正确的方向..

【问题讨论】:

  • 不使用自动布局就不能使用UITableViewAutomaticDimension
  • @dan by autolayout,你指的是 IB Visual 吗?
  • 自动布局是指为视图添加约束。您真的应该为此使用UIStackView,而不是尝试通过设置框架手动堆叠您的视图

标签: ios swift uitableview ios8 constraints


【解决方案1】:

在您的 UITableViewController 中,在 viewDidLoad() 中,尝试:

//set up table view cell dynamic height
tableView.rowHeight = UITableViewAutomaticDimension

在 Storyboard 中,确保您使用 AutoLayout,并且您的单元格元素对超级视图的所有约束都已设置并修复。例如,单元格的顶部文本必须定义其到 Superview 的边距并设置为一个值。

【讨论】:

  • 我上面的回答里写的。
【解决方案2】:

为了让单元格自动调整其高度,您在 contentView 中的内容必须对 contentView 的所有 4 个边缘都有约束。

我自己刚刚使用不同的单元格类型来处理文本、图像和嵌入的推文,并且遇到了您描述的一些问题。我发现最简单的方法是在内容视图中的任何组件周围添加一个视图,然后您可以轻松地将约束添加到该元素周围。

我刚刚快速浏览了一下可以证明这一点的东西,并找到了另一个 SO answer 用一些屏幕截图来解释它。

我今天这样做时使用的参考是This post on Ray Wenderlich's site

【讨论】:

  • 感谢您的来信。我昨天花了一些时间在那个特定的 SO 帖子上。将它的代码复制到我的代码中,并将wordContainer 的约束扩展到其父cell - 我看到cell.frame.size.height 实际上发生了变化,但单元格仍然在44.0 打开,并且子视图丢失它们的间距和堆叠在彼此之上。
  • 您的设置在上面看起来很好,这只是您的限制。您不需要设置任何高度,因为这将根据内容计算,但您需要设置顶部标签和内容视图顶部之间的距离。第二个标签的顶部和顶部标签的底部,然后是第二个标签的底部和内容视图的底部。 (和侧面)
  • 嗯,很有趣。 “距离”是指设置约束还是高度/宽度?​​
  • 元素之间的边距,所以像bottomLabel.top应该距离topLabel.bottom 10px
  • 我在上面更新了我的问题。绝对认为我现在快到了。我向我的wordContainer 添加了一个约束,其中底部和顶部锚点反映了单元格的 contentView。这绝对正确地打开了空间。但现在这些词一个一个地叠在一起。您知道正确间隔它们需要什么限制吗?非常感谢!!
猜你喜欢
  • 2016-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
相关资源
最近更新 更多