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.

Community
  • 1
  • 1
Isuru
  • 30,617
  • 60
  • 187
  • 303
  • 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. @Isuru – Manu Mar 20 '13 at 06:18
  • 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", – Exploring Mar 20 '13 at 06:21

2 Answers2

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.

Kunal
  • 649
  • 4
  • 13
  • 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! :) – Isuru Mar 20 '13 at 06:39
  • 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](http://www21.zippyshare.com/v/44079522/file.html). Hope you can help me out one more time, if its not too much to ask. – Isuru Mar 25 '13 at 11:10
  • It seems the link is broken.. Could you please try sending it again? – Kunal Mar 25 '13 at 11:30
  • 1
    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. – Kunal Mar 25 '13 at 11:42
  • 2
    and with above dont forget to make a change in Bottom space constraint and set constant value to 0 – Kunal Mar 25 '13 at 11:47
  • 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. :D – Isuru Mar 25 '13 at 12:03
  • Glad I am of help to you. Yes you will surely get it when you will star visualising it. :) – Kunal Mar 25 '13 at 12:07
  • Hi Kunal, I've hit another bump regarding this. I've posted a separate question [here](http://stackoverflow.com/q/15893022/1077789), if you can please kindly have a look. Thanks. – Isuru Apr 09 '13 at 04:06
  • Hello Kunal, I have faced the same issue again when working in iOS 7. I have posted a question [here](http://stackoverflow.com/questions/21363529/cant-get-uiscrollview-to-work). Can you please take a look? – Isuru Jan 26 '14 at 14:46
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);
Jsdodgers
  • 5,253
  • 2
  • 20
  • 36