How to adjust the height of a Uiview on Swift 3 according to subviews?

Asked

Viewed 116 times

1

I have a Uiview with subviews, and I need Uiview to increase or decrease according to the size of the subviews, the same idea of a layout with wrap_content height. Does anyone know how to do this on Swift 3? Thanks in advance.

1 answer

0

To have this behavior, you will use the intrinsicContentSize of UIView.

To do so, simply add NSLayoutConstraints from the top to the bottom of your UIView "mother".

For example, imagine we have a UILabel within a UIView. The rectangle around the UILabel represents this UIView:

- - - - - - -
|  UILabel  |  
- - - - - - - 

When using Autolayout, in the case above, as we do not have NSLayoutConstraints applied to UILabel, its point of origin will be 0,0 and the dimension of UIView will be your CGRect frame.

From the moment we add NSLayoutConstraints, for example, to its top, left, right and footer without defining the height of the Uilabel, this behavior changes: the UIView will have the size of UILabel:

- - - - - - -
|     I     |
|--UILabel--|  
|     I     |
- - - - - - - 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.