UIScrollView won't work
Asked Answered
L

2

2

I've been struggling with this all morning for more than 3 hours now and I'm literally going insane!

I'm creating a Storyboard app for iOS 6 using Xcode 4.6. I dragged and dropped a UIScrollView to a ViewController. After designing the viewable part of the Scroll view, using its handles I stretched it vertically and pushed it up a bit so that that part is visible for me to design the rest of the screen. After I was done I positioned the scroll view back to fit the view. But I did not resize the scroll view to match the size of the view from the IB.

I added the following line of code in the viewDidLayoutSubviews method (Used that method instead of viewDidLoad because of Chris' comment here).

self.scrollView.contentSize = self.view.frame.size;

The code gets executed but the scroll bars won't appear and it scrolling does not work at all! I went through almost all the questions and answers here on SO regarding this and tried everything but to no avail.

I attached the runnable Xcode project here as well.

Please tell me what I should do to get this working. I'm eternally frustrated with Apple.

Thank you.

Layer answered 20/3, 2013 at 6:7 Comment(2)
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width,500); Just place a static value and check and please set the delegate connection to your scrollview. @LayerReside
The contentsize for scroll must be more than the frame(height/width) of scrollview otherwise it won't scroll because no need to scroll then, all contents are visible without scrolling. And also are you using "Auto layout",Dromedary
C
5

It is quite easy though. Remove viewDidLayoutSubviews method and replace viewDidLoad with following code. Height 672 is hardcoded for now.. but it will change as per the content in the scrollview.

- (void)viewDidLoad
{
    [super viewDidLoad];
     self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 627);
    self.scrollView.frame = self.view.frame;
}

And in the storyboard, perform following steps 1. Select View and go to size inspector. 2. Select Bottom Space from the contraints. 3. Edit Bottom space constraint and set constant value to 0 (it was -211).

Link is updated source code : http://www54.zippyshare.com/v/37137086/file.html

Hope this helps.

Cryolite answered 20/3, 2013 at 6:25 Comment(9)
Thanks a ton, man! I was pulling my hair out! Instead of just hardcoding the value 627, I replaced it with self.mailAdvancedSearchScrollView.frame.size.height. Its working fine now. Thanks again! :)Layer
Hi Kunal, its me again. I'm stuck with the same issue once again and I've been trying all day to solve it. I've followed everything correctly this time and used the above code. But still it doesn't scroll! This is really frustrating! I uploaded the project here. Hope you can help me out one more time, if its not too much to ask.Layer
It seems the link is broken.. Could you please try sending it again?Cryolite
The problem is in (1.) self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.scrollView.frame.size.height); and (2.).storyboard file. In storyboard You can see that the frame height of the scrollview is 416, that means the scrollview loads on the screen with the height 416 and hence because of first line (1.) refers to the updated height (which is 416 for content height) and hence it is not scrolling. To fix it you need to increase the size of the scrollview's frame in storyboard so that it will cover all the controls.Cryolite
and with above dont forget to make a change in Bottom space constraint and set constant value to 0Cryolite
YES! Got it working! I think I finally understood it a bit. Some more practicing would be good. Thank you so much for helping me out. :DLayer
Glad I am of help to you. Yes you will surely get it when you will star visualising it. :)Cryolite
Hi Kunal, I've hit another bump regarding this. I've posted a separate question here, if you can please kindly have a look. Thanks.Layer
Hello Kunal, I have faced the same issue again when working in iOS 7. I have posted a question here. Can you please take a look?Layer
F
1

UIScrollViews will only scroll if their contentSize is greater than their frame. If it is less than or equal to their frame, they will just act like views.

If you want to test it out, try:

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width,self.scrollView.frame.size.height+200);
Federate answered 20/3, 2013 at 6:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.