Search code examples
iosswiftuiviewcontrolleruistackviewuicontainerview

iOS - Get container view content dynamic height


In my application using ContainerView, which contains below Things

  • StackView
    • UIView
    • TableView
    • UIView

In some cases, I have to hide views, tableview. With below settings, I am able to show the container view with dynamic height

  • view.translatesAutoresizingMaskIntoConstraints = false
  • With UITableView contentSize observer by changing tableview height.

But here I am unable to get the dynamic height of the containerView content.

I have tried below options to get container view content height but no luck

  • containerView.frame.size.height
  • containerView.bounds.size.height
  • stackView.frame.size.height
  • stackView.bounds.size.height
  • using setNeedsLayout() with containterView, stackView before getting height
  • calling layoutIfNeeded() with containterView, stackView before getting height
  • trying to get containView, stackView height in override func viewDidLayoutSubviews(), override func viewWillLayoutSubviews()

Solution

  • Finally found an answer, below are the steps to achieve the solution

    • for StackView we have to set the bottom constraint as >= 0.
    • After hiding any view within the StackView, we have to apply stackView.setNeedsLayout(), stackView.layoutIfNeeded() these on StackView
    • Now if you print the stack view frame it will show the updated frame result.