Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storyboard UIScrollView contentSize?

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.

like image 480
Josh Kahane Avatar asked Apr 02 '13 20:04

Josh Kahane


People also ask

How do I create a scroll view?

In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it. A ScrollView supports Vertical scrolling only, so in order to create a horizontally scrollable view, HorizontalScrollView is used.

How do I use scroll view in interface builder?

You can scroll the scrollview in the Storyboard / Interface builder! Select the view inside scrollview in Document Outline, then scroll using your mouse or trackpad, you can see the view move. You should see the view controller elongated to 1100 pt height now.

What is content layout guide?

The most useful here is Content Layout Guide. Basically it helps to determine the scrollable content size of your UIScrollView 's contents. So in your case you have to have a Content view inside your Scroll View that has equal width with Frame Layout Guide and have its edges pinned to Content Layout Guide.


1 Answers

use ViewDidLayoutSubview

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

UIViewController's method invoke sequence is as below

  • awakeFromNib
  • viewDidLoad
  • viewWillAppear
  • viewWillLayoutSubviews
  • viewDidLayoutSubviews
  • viewDidAppear
like image 87
Ryan Avatar answered Sep 18 '22 15:09

Ryan