我目前在 Interface Builder 的 UIContainerView 中嵌入了一个 UITableViewController。有没有办法将 UIContainerView 的高度设置为内容的高度(使用原型(prototype)单元格)?基本上,我想要的是在我的 UIContainerView 周围有一个 UIScrollView,并为我的 UIContainerView 设置一些自动布局规则,以便根据内部单元格的数量计算高度,父 UIScrollView 相应地调整其滚动区域。
编辑
感谢伟大的答案,最终使用此代码更改框架高度:
alarmsTableViewController.view.layoutIfNeeded()
println(alarmsTableViewController.tableView.contentSize.height)
alarmsContainerView.frame.size.height = alarmsTableViewController.tableView.contentSize.height
最佳答案
需要编程干预:UITableViews 当然是 UIScrollViews,嵌套的 ScrollView 将无法在 AutoLayout 中自行调整大小。这是一个合理的歧义,因为 AutoLayout 不会知道要使框架更小并需要更多滚动,或者框架更大而滚动更少。在您的情况下,您希望后者没有滚动。
解决此问题的最简单方法是为 TableView(或其容器 View )的高度创建约束。这将作用于 tableView 的框架,如果设置为大于任何方向的内容大小(仅在 tableviews 中为垂直方向), ScrollView 将不会滚动。最好在包含 tableView 的 View 中添加/修改它。然后访问 TableView 的 contentSize 属性。实际代码非常简单:
-(void)updateViewConstraints {
self.tableViewHeightLayoutConstraint.constant = self.tableviewInMyScrollView.contentSize.height;
[super updateViewConstraints];
}
如果 scrollview 中的所有其他内容的高度和宽度在 AutoLayout 引擎运行时是固定的,这应该会自动根据其内容调整 scrollview 的大小。
关于ios - 内容的 UIContainerView 高度(嵌入式 UITableViewController),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28411983/