How to adjust the height of a UiView in Swift 3 according to the subviews?

1

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.

    
asked by anonymous 24.05.2017 / 22:46

1 answer

0

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     |
- - - - - - - 
    
07.06.2017 / 15:00