4

This is the same as asking how to achieve wrap_content in iOS.

However, I found no suitable solution. Almost all solutions suggest to override intrinsicContentSize(). What if the view does not know any information about its child's height?

The case is like this:

I want to create a BaseDialogViewController that is responsible for blurring the background and displaying a dialog container. Then if I want to implement a dialog, I will extends BaseDialogViewController and add the dialog content to the container view.

So, the container needs to adjust its height according to the total height of the content inside of it.

How do I achieve this?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Sira Lam
  • 5,179
  • 3
  • 34
  • 68

2 Answers2

1

Just set constraints for all borders from the child view to the container view. You should ensure that the child view can compute its size from its contained elements, by setting constrains for all borders to its child elements, and so on.

The container view should only have additional constraints for its position (e. g. centering in its parent view).

The rest is done by Autolayout. It will calculate the size from the innermost elements with fixed or intrinsic sizes (e.g. labels, text fields, buttons) to the outer elements.

clemens
  • 16,716
  • 11
  • 50
  • 65
0

I realized that if child views height is specified, or has an intrinsic height,

you can set constraint from parent's bottom to child's bottom, and by setting this, the parent's height will be able to vary according to the bottom position of child's view.

I found this out from this answer.

Sira Lam
  • 5,179
  • 3
  • 34
  • 68