Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a stack view or container view wrap contents?

rookie IOS developer here migrating from an Android code base.

In android if we wanted a container that automatically adjusted its height based on its internal contents, we would just do something like the following

<LinearLayout
    android:orientation="vertical"
    android:layout_width="500dp"
    android:layout_height="wrap_content">
    ...
</LinearLayout>

How is this achieved in autolayout through the storyboard? How can I make a stackview or containerview's width or height to be wrap_content?

like image 570
AlanSTACK Avatar asked Nov 07 '22 11:11

AlanSTACK


1 Answers

You can accomplish the same on ios by using UIStackView (Verticl/Horizontal) or UIScrollview

let me explain the whole process in scrollview

1- drag a scrollview to your view controller in storyboard file/Xib

2- give the storyboard leading , trailing , top , bottom constraints to the main view

3- drag a UIView (contentView) inside the scrollView and hook it's leading , trailing , top , bottom constraints to the scrollview

4- control-drag from the contentView to the mainView and select Equal-widths constraint

until this step you have a scrollview that is ready to be wrapped according to the inner size of it's components

5- drag a UILabel and hook it's leading , trailing , top , bottom constraints to the contentView

in runtime change the content of that label and the scrollview will wrap according to the label content

like image 82
Sh_Khan Avatar answered Nov 15 '22 07:11

Sh_Khan