Reputation: 171
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?
Upvotes: 3
Views: 8158
Reputation: 2408
SWIFT 4.0 Update
@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)
}
Upvotes: 0
Reputation: 306
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.
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.
Upvotes: 0
Reputation: 11
Try This
1.scrollView.bounds = YES;
2.scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: YourContentView.frame.size.height);
Upvotes: -1
Reputation: 382
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
Upvotes: 0
Reputation: 171
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!
Upvotes: 7
Reputation: 9346
Try setting the contentSize
in viewDidLayoutSubviews
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.contentSize = CGSize(width: 375, height: 1500)
}
Upvotes: 8
Reputation: 22969
Sounds like uou may not have set the contentSize
for your scroll view. To do that if you're not using Auto layout:
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.
Hope that helps.
Upvotes: 4