具有动态 tableView 高度的模糊布局 [英] Ambiguous layout with dynamic tableView height

查看:16
本文介绍了具有动态 tableView 高度的模糊布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 viewController,我想在里面有两个 1 tableView 和 1 childViewController.

I have a viewController and I want to have two 1 tableView and 1 childViewController inside it.

  • tableView 不可滚动,具有动态单元格高度.(我使用的是 tableView,所以我可以折叠行)
  • childVC 是具有动态单元格高度的可滚动 tableView.

我的约束如下:

NSLayoutConstraint.activate([
   tableView.topAnchor.constraint(equalTo: view.topAnchor),
   tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
   tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
   tableView.bottomAnchor.constraint(equalTo: artistVC.view.topAnchor),

])

NSLayoutConstraint.activate([
   artistVC.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
   artistVC.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
   artistVC.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])

当我这样设置时,只有 childVC 显示在屏幕上.我没有看到 tableView. 我想让 tableView 尽可能多地展开,然后让 childVC 可滚动到底部.

When I setup this way, only the childVC shows on the screen. I don’t see the tableView. I want to allow the tableView to expand as much as it needs and then let the childVC be scrollable to the bottom.

  • 对于我得到的 tableView:高度和可滚动内容大小不明确
  • 对于 childVC 的观点,我得到:高度和垂直位置不明确

但是我不能自己设置高度,因为我不知道 tableViewCell 的高度是多少..

However I can't set the height myself as I don't know what the height of the tableViewCell is..

有什么建议吗?我已经尝试更改内容拥抱和内容压缩阻力,但我没有找到任何运气.

Any suggestions? I've tried changing the content hugging and content Compression Resistance but I didn't find any luck there.

推荐答案

我建议像这样子类化 UITableView:

I suggest subclassing UITableView like this:

class AutomaticHeightTableView: UITableView {

  override var intrinsicContentSize: CGSize {
    return contentSize
  }

  override func reloadData() {
    super.reloadData()
    invalidateIntrinsicContentSize()
  }
}

然后在 Interface Builder 中将 AutomaticHeightTableView 设置为表格视图的类,表格将尝试调整自身大小以适应内容.它将遵循您放置的任何其他约束,因此请确保约束允许它自由增长.

Then in Interface Builder set AutomaticHeightTableView as the class to your table view and the table will try to size itself to fit the content. It will follow any other constraints you placed so make sure the constraints allow it to grow freely.

这篇关于具有动态 tableView 高度的模糊布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆