UIView dynamic height depending on Label Height

后端 未结 6 1005
再見小時候
再見小時候 2021-01-15 05:35

I have a Label which takes dynamicaly some data from database. These data are strings which can sometimes be 3-4-5 rows etc. So this labe is inside a UIView.



        
6条回答
  •  广开言路
    2021-01-15 06:18

    I know this is late answer, but it might help someone else.

    To make the Dynamic height for UIView follow the simple steps in Storyboard

    1. Add a UIView in UIViewController and set your favourite background colour

    1. Now set the following constraints Leading, Top, Trailing and Height(as of now). We can adjust the Height constraint to achieve dynamic height further.
    2. Update Height Constraints as shown below:
    3. Now probably there storyboard will show you inequality constraint ambiguity. But we are going to fix this now. Just add a label inside UIView as shown
    4. Now Set the Constraints for Label Leading, Trailing, Top and Bottom
    5. Hurrah, Now the UIView height will increase based on the label's height. just make the following changes to label

    This technique works with other views inside this UIView. The thing is you must specify bottom constraints for the views present inside this UIView.

提交回复
热议问题