15

I feel like I have touched on every single possible cause for stopping this, but I have a UIScrollView in my Storyboard hooked up with an outlet and in the viewDidLoad I set the contentSize so that I can scroll (yes bigger than my frame size)!

However, whatever I change, I just can't scroll! I have a couple of textfields in my scrollview and bouncing enabled so I can see that when testing its moves up and down with my subviews in it but whatever I set the contentSize to I just can't scroll.

Anything I might be missing/should check? Is this a known issue with UIScrollView being used in a storyboard?

Whats even stranger is, I can do something like this: [scrollView setBackgroundColor:[UIColor blueColor]]; and I have a blue scroll view! But setting content size fails.

Edit

My only code (otherwise scrollview is just dropped into storyboard view controller):

-(void)viewDidAppear:(BOOL)animated
{
    [scrollView setContentSize:CGSizeMake(320, 640)];
}

Logged frame, comes out as expected:

width: 320.00
height: 504.00

Edit 2

Turns out that removing any subviews of the scroll view in my storyboard lets it scroll just fine. If I add any subview to it at all via the storyboard, even a blank brand new UIButton it just won't apply the contentSize/allow scrolling.

halfer
  • 19,824
  • 17
  • 99
  • 186
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253

8 Answers8

48

use ViewDidLayoutSubview

- (void)viewDidLayoutSubviews
{
    [_scrollView setContentSize:CGSizeMake(320, 500)];
}

UIViewController's method invoke sequence is as below

  • awakeFromNib
  • viewDidLoad
  • viewWillAppear
  • viewWillLayoutSubviews
  • viewDidLayoutSubviews
  • viewDidAppear
Ryan
  • 4,799
  • 1
  • 29
  • 56
5

viewDidLoad is not a good place to put code that relies on frame sizes of IB objects. If you log the contentSize of your scroll view in viewDidLoad, you will see that it's (0,0). Move the code (where you set the content size) to viewDidAppear, and it will work properly.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • Attempted, logical move but no luck. – Josh Kahane Apr 02 '13 at 20:49
  • @JoshKahane, It works fine for me, so you must be doing something wrong. You should post the code that you're using now. – rdelmar Apr 02 '13 at 20:50
  • Well thats the thing, I just have dropped my scroll view into my view controller, hooked it up, enabled scrolling, interaction etc, the only code I have is setting the content size. – Josh Kahane Apr 02 '13 at 20:51
  • @JoshKahane,, log scrollView to make sure you hooked it up properly. – rdelmar Apr 02 '13 at 20:55
  • @JoshKahane, also make sure you have [super viewDidAppear:animated]; in your implementation. I don't think that's your problem, but you should always have that. – rdelmar Apr 02 '13 at 20:58
  • @JoshKahane, one other thing. What is the scroll view's frame size? If that's as big as the content size, it won't scroll. – rdelmar Apr 02 '13 at 21:02
  • Logged frame, expected result was given as in original post now. I set my content size larger than this. – Josh Kahane Apr 02 '13 at 21:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27435/discussion-between-rdelmar-and-josh-kahane) – rdelmar Apr 02 '13 at 21:04
  • 1
    you should overrite the viewDidLayoutSubviews method, there all your frames are already set – OscarVGG Oct 10 '13 at 20:09
4

Check these

  • User Interaction enabled
  • Outlet connected
  • Included contentsize greater than bounds
  • scrolling Enabled

eg

scrollView.contentSize = CGSizeMake(320, 640);

My storyboard looks like this for scrollview [working]

enter image description here

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
3

I had exactly the same line of code in viewDidAppear and it did not work

Moved it to viewDidLayoutSubviews and it worked correctly.

[scrollView setContentSize:CGSizeMake(320, 500)];

Thanks trick14 for the answer.

timv
  • 3,346
  • 4
  • 34
  • 43
3

The issue is most probably with Auto Layout. UIScrollView needs special attention when using AutoLayout.

Quick-fix - bind one of the scroll's subviews to the top AND bottom space of it's superview (the scroll view).

Long story: Questions on SO: UIScrollView not scrolling regardless of large contentSize, UIScrollView will not scroll, even after content size set, UIScrollView doesn't use autolayout constraints

Apple's Documentation: https://developer.apple.com/library/ios/technotes/tn2154/_index.html

Community
  • 1
  • 1
Oded Ben Dov
  • 9,936
  • 6
  • 38
  • 53
1

Trip14's answer worked for me. In swift I coded it as:

override func viewDidLayoutSubviews() {

          (self.view as! UIScrollView).contentSize = CGSizeMake(600, 600)
        }
0

This seems to be a similar issue. Other Story

It might be an issue with auto layout or constraints in the storyboard.

Community
  • 1
  • 1
Heckman
  • 398
  • 1
  • 13
  • Seems possible, only I've heard others say Auto Layout doesn't affect them. Thanks for pointing me in this direction though, I'll investigate further. – Josh Kahane Apr 02 '13 at 22:18
0

the best way with the storyboard.:

follow the picture ,best way

Ryan110
  • 710
  • 4
  • 19