内容的 UIContainerView 高度(嵌入式 UITableViewController)
UIContainerView height of content (embedded UITableViewController)
我目前在 Interface Builder 的 UIContainerView 中嵌入了一个 UITableViewController。有没有办法将 UIContainerView 的高度设置为内容的高度(使用原型单元格)?基本上,我想要的是在我的 UIContainerView 周围有一个 UIScrollView,并为我的 UIContainerView 设置一些自动布局规则,以便根据内部单元格的数量计算高度,父 UIScrollView 相应地调整其滚动区域。
编辑
多亏了很好的答案,最终使用以下代码更改了框架高度:
alarmsTableViewController.view.layoutIfNeeded()
println(alarmsTableViewController.tableView.contentSize.height)
alarmsContainerView.frame.size.height = alarmsTableViewController.tableView.contentSize.height
我在我的项目中所做的是在 -(void)updateViewConstraints
中,在您的 ViewController 中更新由 IBOutlet
给出的约束,具体取决于 tableView 中的单元格数量。例如:
-(void)updateViewConstraints{
float newHeigh = ([self.array count] + 1) * 84;
self.topViewHeightConstraint.constant = newHeigh + 540;
[super updateViewConstraints];
}
需要编程干预:UITableViews 当然是 UIScrollViews,嵌套的滚动视图将无法在 AutoLayout 中自行调整大小。这是一个合理的歧义,因为 AutoLayout 不会知道要使框架更小并需要更多滚动,或者框架更大而滚动更少。在您的情况下,您希望后者没有滚动。
解决此问题的最简单方法是为 TableView(或其容器视图)的高度创建约束。这将作用于 tableView 的框架,如果设置为大于任何方向的内容大小(在 tableviews 中仅垂直),滚动视图将不会滚动。最好将 it/modify 添加到包含 table 视图的视图中。然后访问 table 视图的 contentSize 属性。实际代码很简单:
-(void)updateViewConstraints {
self.tableViewHeightLayoutConstraint.constant = self.tableviewInMyScrollView.contentSize.height;
[super updateViewConstraints];
}
如果滚动视图中的所有其他内容的高度和宽度在 AutoLayout 引擎运行时是固定的,这应该会根据其内容自动调整滚动视图的大小。
我目前在 Interface Builder 的 UIContainerView 中嵌入了一个 UITableViewController。有没有办法将 UIContainerView 的高度设置为内容的高度(使用原型单元格)?基本上,我想要的是在我的 UIContainerView 周围有一个 UIScrollView,并为我的 UIContainerView 设置一些自动布局规则,以便根据内部单元格的数量计算高度,父 UIScrollView 相应地调整其滚动区域。
编辑
多亏了很好的答案,最终使用以下代码更改了框架高度:
alarmsTableViewController.view.layoutIfNeeded()
println(alarmsTableViewController.tableView.contentSize.height)
alarmsContainerView.frame.size.height = alarmsTableViewController.tableView.contentSize.height
我在我的项目中所做的是在 -(void)updateViewConstraints
中,在您的 ViewController 中更新由 IBOutlet
给出的约束,具体取决于 tableView 中的单元格数量。例如:
-(void)updateViewConstraints{
float newHeigh = ([self.array count] + 1) * 84;
self.topViewHeightConstraint.constant = newHeigh + 540;
[super updateViewConstraints];
}
需要编程干预:UITableViews 当然是 UIScrollViews,嵌套的滚动视图将无法在 AutoLayout 中自行调整大小。这是一个合理的歧义,因为 AutoLayout 不会知道要使框架更小并需要更多滚动,或者框架更大而滚动更少。在您的情况下,您希望后者没有滚动。
解决此问题的最简单方法是为 TableView(或其容器视图)的高度创建约束。这将作用于 tableView 的框架,如果设置为大于任何方向的内容大小(在 tableviews 中仅垂直),滚动视图将不会滚动。最好将 it/modify 添加到包含 table 视图的视图中。然后访问 table 视图的 contentSize 属性。实际代码很简单:
-(void)updateViewConstraints {
self.tableViewHeightLayoutConstraint.constant = self.tableviewInMyScrollView.contentSize.height;
[super updateViewConstraints];
}
如果滚动视图中的所有其他内容的高度和宽度在 AutoLayout 引擎运行时是固定的,这应该会根据其内容自动调整滚动视图的大小。