iOS - 获取容器视图内容动态高度
iOS - Get container view content dynamic height
在我使用 ContainerView 的应用程序中,其中包含以下内容
- 堆栈视图
- UIView
- 表格视图
- UIView
在某些情况下,我不得不隐藏视图,tableview。通过以下设置,我可以显示具有动态高度的容器视图
view.translatesAutoresizingMaskIntoConstraints = false
- 与
UITableView
contentSize
observer 通过改变 tableview 高度。
但是这里无法获取containerView内容的动态高度
我尝试了以下选项来获取容器视图内容高度,但没有成功
containerView.frame.size.height
containerView.bounds.size.height
stackView.frame.size.height
stackView.bounds.size.height
- 在获取高度之前将
setNeedsLayout()
与containterView、stackView结合使用
- 在获取高度之前使用 containterView、stackView 调用
layoutIfNeeded()
- 正在尝试获取
override func viewDidLayoutSubviews()
中的 containView、stackView 高度,override func viewWillLayoutSubviews()
终于找到答案了,下面是实现解决方案的步骤
- 对于 StackView,我们必须将底部约束设置为
>= 0
。
- 在 StackView 中隐藏任何视图后,我们必须在 StackView 上应用
stackView.setNeedsLayout(), stackView.layoutIfNeeded()
这些
- 现在,如果您打印堆栈视图框架,它将显示更新后的框架结果。
在我使用 ContainerView 的应用程序中,其中包含以下内容
- 堆栈视图
- UIView
- 表格视图
- UIView
在某些情况下,我不得不隐藏视图,tableview。通过以下设置,我可以显示具有动态高度的容器视图
view.translatesAutoresizingMaskIntoConstraints = false
- 与
UITableView
contentSize
observer 通过改变 tableview 高度。
但是这里无法获取containerView内容的动态高度
我尝试了以下选项来获取容器视图内容高度,但没有成功
containerView.frame.size.height
containerView.bounds.size.height
stackView.frame.size.height
stackView.bounds.size.height
- 在获取高度之前将
setNeedsLayout()
与containterView、stackView结合使用 - 在获取高度之前使用 containterView、stackView 调用
layoutIfNeeded()
- 正在尝试获取
override func viewDidLayoutSubviews()
中的 containView、stackView 高度,override func viewWillLayoutSubviews()
终于找到答案了,下面是实现解决方案的步骤
- 对于 StackView,我们必须将底部约束设置为
>= 0
。 - 在 StackView 中隐藏任何视图后,我们必须在 StackView 上应用
stackView.setNeedsLayout(), stackView.layoutIfNeeded()
这些 - 现在,如果您打印堆栈视图框架,它将显示更新后的框架结果。