Search code examples
iosswiftuiviewuiscrollview

How to add Dynamic label inside UIScrollView in ios?


Hierarchy of a ViewController

ScrollView 
    |_ _ UIView (Height and width equal to its superView)
                  |_ _ UIView (Set Height to 150 and I put a label inside this view and set number of lines to 0)
                  |_ _ UIView (Align bottom to top view with the height of 150)

If I keep the label.text too much then it's going behind the bottom view like this:

enter image description here

How can I keep the constant vertical distance between the second view and first view? So that it won't distort the UI. I want to achieve this

enter image description here

The approach I am using won't work because I am providing the fixed height for both UIView. Is there any way that we can handle the dynamic length of label in a UIScrollView?


Solution

  • ScrollView 
        |_ _ UIView (Height and width equal to its superView)
                      |_ _ UIView (Set Height to greater than or equal 150 and I put a label inside this view and set number of lines to 0)
                      |_ _ UIView (Align bottom to top view with the height of 150)
    

    Below are the constraints set to the view View in the Storyboard

    the view reduces to a standard value of 150 according to the text of the label

    view reduced to standard value