1

I am trying to achieve a pretty complex view hierarchy. My idea is to build a UI similar to the AirPods and HomePod configurator sheet showing from the bottom part of the screen.

I have a fullscreen UIViewController which has a child view controller of type UINavigationController. In this navigation controller, I will push and manager all my view controllers. Now I have a view controller which has a UIStackView with some content in it, and it's text has variable size. Currently, the navigationController has a fixed height constraint but obviously I want to change that height based on the size of the content of the visible view controller currently in the navigationController stack.

Here is a picture of what the view hierarchy currently looks like. enter image description here

I tried different approaches like setting the compression resistance priority on the labels to 1000 to avoid them clipping but the container view has a fixed height, so they don't grow in any way. Basically, I need a way to figure the content of the UIStackView and then pass that height to the top most view controller which changes the height constant of the UINavigationController.

Did anyone implement a system to manage this kind of view hierarchy?

BalestraPatrick
  • 9,944
  • 4
  • 30
  • 43

1 Answers1

0

Found a solution to the problem:

let size = stackView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
let viewControllerSize = CGSize(width: self.view.frame.width, height: size.height + 40)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DidChangeContentSize"), object: viewControllerSize)
stackView.layoutIfNeeded()

40 is the spacing around the UIStackView in my case. The top most view controller observes the notification and changes the container view controller height constraint constant. Yes, you could delegates to pass up this information instead of NotificationCenter.

BalestraPatrick
  • 9,944
  • 4
  • 30
  • 43