xcode 如何在 Swift 中设置 UIScrollView 高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45293983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set UIScrollView Height in Swift
提问by christ2702
I'm using swift to build an application. I want to add some content to my view controller using storyboard.
我正在使用 swift 来构建一个应用程序。我想使用情节提要向我的视图控制器添加一些内容。
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)
为了您的信息,请忽略“评论家”和提交按钮之间的空白区域,因为实际上它们之间有 UITextView 但不可见(仍然尝试添加一些边框)
these are my storyboard preview
这些是我的故事板预览
回答by iOS Geek
1) ScrollView Contraints
1) ScrollView 约束
2) ScrollView -> contentView Constraints to scroll View same as above image
2) ScrollView -> contentView 约束滚动视图与上图相同
3) now ContentView width and Height Constraints to main View [SuperView in which ScrollView is embedded] and constraints will be as follows.
3)现在ContentView的宽度和高度约束到主视图[嵌入ScrollView的SuperView]和约束如下。
4) now click on the EqualWidth to View [Third constraint from top]and edit it as in step 6
4) 现在单击 EqualWidth 以查看 [从顶部开始的第三个约束] 并按照步骤 6 进行编辑
5) contentView Width Contraint
5) contentView 宽度约束
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
6) ContentView 高度约束 // 设置优先级 [必须] 。在这里,您需要将菜单中的第一项和第二项更改为如图所示的 First as - ContentView.Height 和第二个 as - View.height 并将优先级设置为 250 之后,虚线将沿着 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
7) 现在在 contentView 中添加 [UIView, labels, textfields] 之类的内容,并将约束作为 Top upperMost 视图顶部空间添加到 contentView [就像我有]DoubleRight imageView
and constraints for my DoubleRight imageView are
我的 DoubleRight imageView 的约束是
look for the Top space margin its given a top space 20 points
寻找给定顶部空间的顶部空间边距 20 分
and same you need to do for the last item you will be adding in ContentView like I have uiView
同样你需要为你将添加到 ContentView 的最后一个项目做同样的事情,就像我有 uiView
add bottom space from this respective view to your superView[ContentView] and my constraints are:
将此相应视图的底部空间添加到您的 superView[ContentView] 中,我的约束是:
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
就像 Flow 会
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
使用 uiView, uiimageViews 它们的高度必须是固定的
回答by iDeveloper
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.
回答by user11027368
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)