I have a UIView with subviews, and I need the UIView to increase or decrease according to the size of the subviews, the same idea of a layout with height wrap_content. Does anyone know how to do this in Swift 3? Thank you in advance.
I have a UIView with subviews, and I need the UIView to increase or decrease according to the size of the subviews, the same idea of a layout with height wrap_content. Does anyone know how to do this in Swift 3? Thank you in advance.
To have this behavior, you will use intrinsicContentSize
of UIView
.
To do this, simply add NSLayoutConstraint
s from the top to the bottom of your UIView
"mother".
For example, imagine that we have UILabel
within UIView
. The rectangle around UILabel
represents this UIView
:
- - - - - - -
| UILabel |
- - - - - - -
When using AutoLayout, in the above case, since we do not have NSLayoutConstraint
s applied to UILabel
, its point of origin will be 0,0
and the dimension of UIView
will be its CGRect
frame. / p>
From the moment we add NSLayoutConstraint
s, for example , to its top, left, right, and footer without setting UILabel height , this behavior changes : UIView
will have the dimension of UILabel
:
- - - - - - -
| I |
|--UILabel--|
| I |
- - - - - - -