所以我有一个scrollView
, 里面scrollView
是一个stackView
包含views
屏幕中的每个。
问题是我stackView
有很多可隐藏的,views
所以我需要containsVieww's height
根据我的stackView's height
我的伪代码应该是这样的:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print(stackView.bounds.height)
// do logic to change contentView size here
}
func setupView(){
scrollView.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
scrollView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
scrollView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
scrollView.backgroundColor = .white
scrollView.contentSize = CGSize(width: view.frame.width, height: view.frame.height)
scrollView.addSubview(stackView)
stackView.setArrangedSubView(views: [label1, label 2, label 3, label 4, ..., label n]
[label1, label 2, label 3].isHidden = true
}
func onHitButton(){
if isHidden {
[label1, label 2, label 3].isHidden = false
isHidden = false
} else {
[label1, label 2, label 3].isHidden = true
isHidden = true
}
print(stackView.bounds.height) // still return the ex height
}
这是问题所在:
首先init
,我的[label1,2,3].isHidden = true
,我stackViewHeight
的是500
当 myonHitButton
被调用时, my [label1,2,3].isHidden = false
, mystackViewHeight
仍然是,500
但屏幕显示正确,这些labels
现在可见并且 mystackView
被拉伸。当然,我scrollView
的 is 显示不正确。
然后我onHitButton
再次点击我的,那些labels
是隐藏的,我stackView
的在屏幕上缩小但stackViewHeight
返回了850
?
应该是反过来的。
我也试过on another call 并返回正确print
的?好像叫得太早了。height
button
height
viewDidLayoutSubviews
总结一下:stackView
在它自己调整大小之前返回高度