简体   繁体   中英

How to set UIScrollView Height in Swift

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

这是视图的顶部

滚动后(查看按钮提交后的空白内容)

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

故事板1

故事板2

1) ScrollView Contraints

在此输入图像描述

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

在此输入图像描述

5) contentView Width Contraint

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

and constraints for my DoubleRight imageView are

在此输入图像描述

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

在此输入图像描述

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

在此输入图像描述

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

It may be helped

your_scrollView.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
your_scrollView.contentSize = CGSize(width: 375, height: 1000) // You can set height, whatever you want.

You can try :

   your_scrollView.frame = CGRect(x: 0, y: 0, width: 375, height: 667)
   your_scrollView.contentSize = CGSize(width: 375, height: self.view.frame.size.hight)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM