动态tableView高度的布局不明确 [英] Ambiguous layout with dynamic tableView height

查看:81
本文介绍了动态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我得到:Height and scrollable content size are ambiguous
  • 对于childVC的视图,我得到:Height and vertical positions are ambiguous
  • for the tableView I get: Height and scrollable content size are ambiguous
  • for the childVC’s view I get: Height and vertical positions are ambiguous

但是我自己无法设置高度,因为我不知道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天全站免登陆