Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using a UIStackView within a UIScrollView and I want to determine the height of the stack view so I can programmatically adjust the height of the scrollview to accomodate when subviews are hidden.

I am using

stackView.frame.height 

to determine stack view height. This is called in viewDidLoad() but it is always the same height value from the storyboard no matter if subviews are hidden or not.

How do I determine height?

like image 994
Khoury Avatar asked Apr 04 '18 02:04

Khoury


1 Answers

After making layout changes to any view, it has to recalculate it's frame after the function is completed. To get the updated frame right away, call:

stackView.layoutIfNeeded()

Example:

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

See the documentation for more details.

like image 120
tktsubota Avatar answered Nov 02 '22 14:11

tktsubota