如何在隐藏子视图后以编程方式确定 UIStackView 的高度 swift

How to programmatically determine the height of UIStackView after a subview has been hidden swift

我在 UIScrollView 中使用 UIStackView,我想确定堆栈视图的高度,以便我可以通过编程方式调整滚动视图的高度,以适应隐藏子视图的情况。

我正在使用

stackView.frame.height 

确定堆栈视图高度。这在 viewDidLoad() 中被调用,但无论是否隐藏子视图,它始终是情节提要中的相同高度值。

如何确定身高?

对任何视图进行布局更改后,它必须在功能完成后重新计算其框架。要立即获取更新的帧,请调用:

stackView.layoutIfNeeded()

示例:

print(stackView.frame.height) // old height
subview1.isHidden = true
print(stackView.frame.height) // still old height
stackView.layoutIfNeeded()
print(stackView.frame.height) // new height

有关详细信息,请参阅 the documentation