具有动态 tableView 高度的模糊布局

2023-09-09移动开发问题
5

本文介绍了具有动态 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 高度的模糊布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?
How can I implement a virtual joystick for a cocos2d game outside the cocos2d environment?(如何在 cocos2d 环境之外实现 cocos2d 游戏的虚拟摇杆?)...
2024-08-12 移动开发问题
13