How to set UIScrollView Height in Swift

17,101

1) ScrollView Contraints

enter image description here

2) ScrollView -> contentView Constraints to scroll View same as above image

3) now ContentView width and Height Constraints to main View [SuperView in which ScrollView is embedded] and constraints will be as follows.

4) now click on the EqualWidth to View [Third constraint from top]and edit it as in step 6

enter image description here

5) contentView Width Contraint

enter image description here

6) ContentView Height Constraint // set priority [must] . here you need to change the first item and second item in the menu to as shown First as - ContentView.Height and second as - View.height and set priority to 250 after this a dotted line will appear in storyboard along the contentView

enter image description here

7) now add the content like [UIView, labels, textfields] in contentView and add constraints as Top upperMost view top space to contentView [like I have]DoubleRight imageView

enter image description here

and constraints for my DoubleRight imageView are

enter image description here

look for the Top space margin its given a top space 20 points

and same you need to do for the last item you will be adding in ContentView like I have uiView

enter image description here

add bottom space from this respective view to your superView[ContentView] and my constraints are:

enter image description here

after you had initialed all these steps results will be as Expected for every Screen size no need to change height additionally for screen sizes

Note : - [all the views must be connected to each other with top and bottom parameter]

like Flow will be

View1 - top to contentView and bottom to View2
View2 - top to View1 and bottom to view3
View3 [Last view] - top to View2 and bottom to contentView as shown 

using uiView, uiimageViews their heights must be fixed

Share:
17,101
christ2702
Author by

christ2702

I'm a programmer, a system analyst, a network administrator, a consultant, etc from Indonesia. I've graduated from University of Surabaya. My academic major is Computer Science with Artificial Intelligence concentration. I like to do some research about developing a program or just analyst my client's needed. I'm still a beginner who needs guidance and help from others who are more experienced. So please, don't hesitate to ask or help me, if I make a mistake. Thank you

Updated on June 13, 2022

Comments

  • christ2702
    christ2702 almost 2 years

    I'm using swift to build an application. I want to add some content to my view controller using storyboard.

    I already put everything inside the scrollview and set the specific height, but however when I run the application, the scrollview always set longer than the button in the bottom of the view

    I want the scroll view stop right after the button

    Please kindly help me how to solve this problem

    thank you

    this is the top of the view

    after scrolling (see the blank content after button submit)

    after scrolling

    for your information, please ignore the white space between 'critics' and submit button, because actually there's UITextView between them but not visible (still try to add some border)

    these are my storyboard preview

    storyboard 1

    storyboard 2