滚动视图 ios 8 Xcode 不滚动 - Swift
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30033886/
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
Scroll View ios 8 Xcode not Scrolling - Swift
提问by FreeTheStudents
I am very new to Swift and developing on ios but I cannot find a way to make the UIScrollView to scroll down and stay down. I have been trying tons of tutorials over this and still nothing. I have a ContentView element inside of my ScrollView element. This ContentView has all of my boxes that I want it to scroll through but it does not scroll. It does though bounce if that makes any difference...can anyone send me in the right direction?
我对 Swift 和在 ios 上开发非常陌生,但我找不到一种方法让 UIScrollView 向下滚动并保持向下。我一直在尝试大量的教程,但仍然没有。我的 ScrollView 元素中有一个 ContentView 元素。这个 ContentView 有我希望它滚动的所有框,但它不滚动。如果这有什么不同,它确实会反弹......有人可以向我发送正确的方向吗?
回答by iAnurag
Try setting the contentSize
in viewDidLayoutSubviews
尝试设置contentSize
在viewDidLayoutSubviews
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.contentSize = CGSize(width: 375, height: 1500)
}
回答by FreeTheStudents
I finally figured it out after 2 days and it was very simple. All I did was set the scrollView to the size of the screen but then set the content view height at 1500px and it started working. Thanks everyone!
2天后我终于想通了,这很简单。我所做的只是将 scrollView 设置为屏幕的大小,然后将内容视图高度设置为 1500px 并开始工作。谢谢大家!
回答by ABakerSmith
Sounds like uou may not have set the contentSize
for your scroll view. To do that if you're not using Auto layout:
听起来你可能没有contentSize
为你的滚动视图设置 。如果您不使用自动布局,请执行以下操作:
scrollView.contentSize = // The size of your content.
If you are using Auto layout you need to make sure you have NSLayoutConstraints
from each edge of your content to each corresponding edge of your UIScrollView
. By doing this the content size of your UIScrollView
will be set automatically.
如果您使用自动布局,您需要确保NSLayoutConstraints
从内容的每个边缘到UIScrollView
. 通过这样做,您的内容大小UIScrollView
将自动设置。
Hope that helps.
希望有帮助。
回答by Vimal
In scrollview, scroll happens only if the scrollview has enough space to scroll its content.
在滚动视图中,只有当滚动视图有足够的空间来滚动其内容时才会发生滚动。
You said the scrollview and its content view have the same dimensions. Then it won't scroll. Scrollview.contentSize should be at least double the size of scrollview if you want it to scroll.
你说滚动视图和它的内容视图具有相同的尺寸。然后它不会滚动。如果你想让它滚动,Scrollview.contentSize 应该至少是 scrollview 大小的两倍。
Hint: scrollview dimension should not be greater than the iPhone screen size. And scrollview content size should be greater than the scrollview. Scroll view scrolls through its contentview.
提示:滚动视图尺寸不应大于 iPhone 屏幕尺寸。并且滚动视图的内容大小应该大于滚动视图。滚动视图滚动其内容视图。
回答by Xcodian Solangi
SWIFT 4.0 Update
SWIFT 4.0 更新
@IBOutlet weak var myScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
myScrollView.alwaysBounceVertical = true
myScrollView.alwaysBounceHorizontal = true
myScrollView.isScrollEnabled = true
myScrollView.contentSize = CGSize(width: 375, height: 1000)
}
回答by Jeremy Bader
Just wanted to add a note to FreeTheStudentsAnswer in case anyone had a similar issue to me - I set the scrollView to the size of the screen and then set the content view height at a larger value and it started working only when I set these in the viewDidAppear instead of viewDidLoad. Not sure if that will help anyone, but that fixed my issue
只是想在 FreeTheStudentsAnswer 中添加一个注释,以防有人遇到与我类似的问题 - 我将 scrollView 设置为屏幕的大小,然后将内容视图高度设置为更大的值,并且只有当我在viewDidAppear 而不是 viewDidLoad。不确定这是否会帮助任何人,但这解决了我的问题
回答by Rameshios
Try This
尝试这个
1.scrollView.bounds = YES;
2.scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: YourContentView.frame.size.height);