Can you get a UITableView#39;s intrinsic content size to update based on the number of rows shown if scrolling is disabled?(如果禁用滚动,您能否根据显示的行数来更新 UITableView 的内在内容大小?)
问题描述
我们的 UI 有一部分是一小部分标签,旁边有颜色样本.我接手的设计在布局中有六个这样的硬编码,即使实际数据是动态的,这意味着如果我们只需要显示三个,我们必须显式隐藏三个,这也会破坏页面的平衡.更糟糕的是,这些项目"中的每一个实际上都由几个子视图组成,因此一个包含六个硬编码项目的屏幕有十八个 IBOutlets.
We have a portion of our UI which is a small list of labels with color swatches next to them. The design I'm taking over has six of these hard-coded in the layout even though the actual data is dynamic, meaning if we only need to show three, we have to explicitly hide three, which also throws off the balance of the page. Making matters worse is each one of those 'items' is actually made up of several sub-views so a screen with six hard-coded items has eighteen IBOutlets.
我想做的是改为使用 UITableView
来表示屏幕的这一小部分,并且由于它不会滚动,我想知道您是否可以使用 AutoLayout 来配置UITableView
的内在内容高度取决于行数.
What I'm trying to do is to instead use a UITableView
to represent this small portion of the screen, and since it won't scroll, I was wondering if you can use AutoLayout to configure the intrinsic content height of the UITableView
to be based on the number of rows.
目前我有一个测试页面,其中 UITableView
垂直限制在中心,但没有高度限制,因为我希望表格的内在内容大小反映可见行.我还禁用了在桌子上滚动.当我重新加载表时,我调用 updateConstraints.但是表格仍然没有调整大小.
Currently I have a test page with a UITableView
vertically constrained to the center, but without a height constraint because I am hoping to have the table's intrinsic content size reflect the visible rows. I have also disabled scrolling on the table. When I reload the table, I call updateConstraints. But the table still does not resize.
注意:我们不能使用 UIStackView(它本来是完美的),因为我们必须以 iOS8 为目标,并且直到 iOS9 才引入,因此这个解决方案.
Note: We can't use a UIStackView (which would have been perfect for this) because we have to target iOS8 and that wasn't introduced until iOS9, hence this solution.
有没有人能够做类似我们需要的事情?
Has anyone been able to do something similar to our needs?
推荐答案
好吧,与 UITextView
不同,它看起来不像 UITableView
曾经返回基于固有大小的在可见行上.但这并不是通过子类来实现的,特别是如果只有一个部分,没有页眉或页脚,并且行的高度是固定的.
Ok, so unlike UITextView
, it doesn't look like UITableView
ever returns an intrinsic size based on the visible rows. But that's not that big a deal to implement via a subclass, especially if there's a single section, no headers or footers, and the rows are of a fixed height.
class AutoSizingUiTableView : UITableView
{
override func intrinsicContentSize() -> CGSize
{
let requiredHeight = rowCount * rowHeight
return CGSize(width: UIView.noIntrinsicMetric, height: CGFloat(requiredHeight))
}
}
我会让读者自己弄清楚如何获得自己的rowCount
.如果您有可变高度、多个部分等,则相同.您只需要更多逻辑.
I'll leave it up to the reader to figure out how to get their own rowCount
. The same if you have variable heights, multiple sections, etc. You just need more logic.
通过这样做,它可以很好地与 AutoLayout 配合使用.我只是希望它能自动处理.
By doing this, it works great with AutoLayout. I just wish it handled this automatically.
这篇关于如果禁用滚动,您能否根据显示的行数来更新 UITableView 的内在内容大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果禁用滚动,您能否根据显示的行数来更新 UITableView 的内在内容大小?
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01