I
I
Ivan Vorobei2020-06-10 19:55:00
Swift
Ivan Vorobei, 2020-06-10 19:55:00

How to make self sizing for child controller view?

I will add a child controller to the parent controller, I will add a child view.

addChild(childController)
view.addSubview(childController.view)
childController.didMove(toParent: self)


The size of a child controller depends on the content in it. The parent is not aware of the content in the child.
How to make a layout so that the height of the child controller changes for a certain content width?

It works with auto layout out of the box, I did not find an implementation on frames.

PS Redefine sizeThatFit will not work, because it is a view controller.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Vorobei, 2020-06-11
@ivanvorobei

To do this on a frame, you need:

  1. at the end of the recalculation of dimensions, set in the child
    preferredContentSize
  2. When setting the size, the method on the parent will be calledpreferredContentSizeDidChange
  3. In the method call, recalculate the layout in the parent, knowing the desired height of the child. There will be no call cycle, notification is not called if the size matches.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question