28

I have a scrollview inside which i have 20 UItextviews. The scrollview is not working. I have set the following in viewdidload.

self.MainScroll.contentSize = CGSizeMake(320, 1800);

Still it doesn't scroll. However, if i give bounce vertically, it just bounces. My scrollview is a child of the main UIview of dimension 320*600. Please guide how to enable the scroll!!

Community
  • 1
  • 1
bharath
  • 953
  • 4
  • 17
  • 30

2 Answers2

102

There are two ways you can get the scrolling to work.

Approach 1 (with code):

1) Pin UIScrollView to the sides of its parent view, as mentioned below.

enter image description here

2) Set content size of your scroll view in viewDidLayoutSubviews:

- (void)viewDidLayoutSubviews {
    self.MainScroll.contentSize = CGSizeMake(320, 1800);
}

Approach 2 (pure IB, no code required):

1) Setting contentSize is not required if using AutoLayout. You need to pin your UIScrollView to the parent view as mentioned below:

enter image description here

2) Then add another UIView inside UIScrollView to act as a content view and pin it to the UIScrollView and move all controls inside this content view:

enter image description here

3) Pin content view to its parent scroll view as mentioned below:

enter image description here

4) Set your UIViewController's Simulated Metrics to Freeform (this is important):

enter image description here

5) Size your content UIView to your desired height (obviously important too):

enter image description here

Apple article explaining UIScrollView and AutoLayouts: https://developer.apple.com/library/content/technotes/tn2154/_index.html

Cœur
  • 37,241
  • 25
  • 195
  • 267
Yas Tabasam
  • 10,517
  • 9
  • 48
  • 53
  • No mate, not working, can anyone share a link or a proj where you have accomplished the same so that it would be a great reference!! – bharath Dec 11 '13 at 05:04
  • 1
    I have modified my answer with step by step instructions, please let me know if it still does not works! – Yas Tabasam Dec 12 '13 at 00:57
  • 3
    Finally! Thank you Yas! Your excellent post put an end to my three day quest to understand the Apple document about scrollviews and autolayout ;) – Andreas Øverland Jan 16 '14 at 13:18
  • Yas: As you mentioned I can not add new constraints as they are disabled when I try to pin UIScrollView. – rohan-patel Mar 29 '14 at 22:10
  • Alright got it working. It was because my Scrollview was not a subview of UIView. I directly added UIScrollView to UIViewController. – rohan-patel Mar 29 '14 at 22:34
  • +1 for you after 3 hours of frustrating attempts got it worked. – rohan-patel Mar 29 '14 at 22:35
  • 1
    implementing viewDidLayoutSubviews was the key. contentSize set in viewDidLoad was ignored, thanks (I got this working without autolayout) – Anton Tropashko Sep 23 '14 at 06:58
  • 2
    +1 for viewDidLayoutSubviews.... For static layouts (nibs) that code can even go in viewDidLoad. For dynamically-built layouts I had it in viewWillAppear, which is fine in iOS8, but not in iOS7. viewDidLayoutSubviews is perfect for both. Cheers – Peter Oct 29 '14 at 16:15
  • The key bit I was missing was not pinning the subviews inside the scrollview to *all* sides. Until I did that the scrollview didn't know how big the content was and thus didn't scroll. – Paul Sturgess Nov 25 '14 at 17:13
  • Thanks for too much explanation – Shahzaib Maqbool Jul 05 '16 at 19:05
-1

Update the content size after some delay as below.

- (void)viewDidLayoutSubviews {
    [self performSelector:@selector(updateContentSize)
               withObject:nil
               afterDelay:0.25];

}

-(void)updateContentSize{
    UIView *viewLast = [viewContent viewWithTag:100];
    scrollViewAd.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, CGRectGetMaxY(viewLast.frame));
}
pkc456
  • 8,350
  • 38
  • 53
  • 109