Ambiguous layout with dynamic tableView height(具有动态 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 高度的模糊布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:具有动态 tableView 高度的模糊布局
基础教程推荐
- iOS4 创建后台定时器 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
