4
votes

My UIScrollView is a ~4500px horizontal view that the user needs to scroll horizontally through to view the content.

I have set it up as follows:

- (void)viewDidLoad
{

    [super viewDidLoad];

    sview.frame = CGRectMake(0, 0, 568, 320);
    sview.contentSize = CGSizeMake(4500, 320);
    [sview setScrollEnabled:YES];
}      

Yet the scroll view does nothing. Is there something obvious I missed? i've tried literally every tutorial on the web.

5
do have that as well. still not scrolling.nworksdev
where do you add the subview(s) which represent the content to be scrolled?FluffulousChimp
You are adding scrollview programatically or ?Manu
set the delegate method, unless your using xibs then the UIScrollView delegate should have been linked to the file owner....jsetting32
It will scroll horizontalyVineesh TP

5 Answers

5
votes

I got similar issue. I did following modifications and the scrollView started scrolling for me:

  1. Select to check the 'Bounce Horizontally' property for UIScrollView in xib.
  2. Move the code following code to viewDidAppear instead of viewDidLoad:
-(void) viewDidAppear:(BOOL)animated
{

    [super viewDidAppear:animated];
    sview.frame = CGRectMake(0, 0, 568, 320);
    sview.contentSize = CGSizeMake(4500, 320);
    [sview setScrollEnabled:YES];

}

I think this should help you.

4
votes

I've explained it here, but there are so many answers to this problem that suggests turning off Auto Layout. That fixes the problem but that's not really the correct solution. Here's my answer:


Turning Auto Layout works, but that's not the solution. If you really need Auto Layout, then use it, if you don't need it, turn it off. But that is not the correct fix for this solution.

UIScrollView works differently with other views in Auto Layout. Here is Apple's release note on Auto Layout, I've copied the interesting bit:

Here are some notes regarding Auto Layout support for UIScrollView:

  • In general, Auto Layout considers the top, left, bottom, and right edges of a view to be the visible edges. That is, if you pin a view to the left edge of its superview, you’re really pinning it to the minimum x-value of the superview’s bounds. Changing the bounds origin of the superview does not change the position of the view.
  • The UIScrollView class scrolls its content by changing the origin of its bounds. To make this work with Auto Layout, the top, left, bottom, and right edges within a scroll view now mean the edges of its content view.
  • The constraints on the subviews of the scroll view must result in a size to fill, which is then interpreted as the content size of the scroll view. (This should not be confused with the intrinsicContentSize method used for Auto Layout.) To size the scroll view’s frame with Auto Layout, constraints must either be explicit regarding the width and height of the scroll view, or the edges of the scroll view must be tied to views outside of its subtree.
  • Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view’s subtree, such as the scroll view’s superview.

Apple then goes on to show example of how to correctly use UIScrollView with Auto Layout.

As a general rule, one of the easiest fix is to create a constraint between the element to the bottom of the UIScrollView. So in the element that you want to be at the bottom of the UIScrollView, create this bottom space constraint:

enter image description here

Once again, if you do not want to use Auto Layout, then turn it off. You can then set the contentSize the usual way. But what you should understand is that this is an intended behaviour of Auto Layout.

1
votes

First of all you have to add some content to UIScrollSiew as subview for scrolling,without content on UIScrollView how can you scroll?. Here is what i did,just add UIImageView to UIScrollView as subview of size same as size of UIScrollView...

In viewDidLoad method try the following code..

-(void)viewDidLoad
{
     UIScrollView *scroll=[[UIScrollView alloc] init];
     scroll.frame=CGRectMake(0, 0, 320, 460);
     UIImageView *imageView=[[UIImageView alloc] init];
     imageView.frame=CGRectMake(0, 0, 320,460);
     imageView.image=[UIImage imageNamed:@"chiranjeevi.jpeg"];
     scroll.contentSize = CGSizeMake(4500, 460);
     [scroll setScrollEnabled:YES];
     [scroll addSubview:imageView];
     [self.view addSubview:scroll];

}

I tested this code it works well.I hope this code will be helpful to you..

0
votes

I assume you are adding UISrollingView in your Xib file. This will work for you.

sview.delegate = self;
sview.backgroundColor=[UIColor clearColor];
[sview setCanCancelContentTouches:NO];
sview.indicatorStyle = UIScrollViewIndicatorStyleWhite;
sview.clipsToBounds = YES;
sview.scrollEnabled = YES;
sview.contentSize = CGSizeMake(320,570);
CGPoint topOffset = CGPointMake(0,0);
[sview setContentOffset:topOffset animated:YES];

Also, make sure to give IBOutlet connection in your Xib file.

-1
votes

I also faced the same issue.I added the scroll view in xib.I also added some subviews to this scroll view. The scroll view would stop scrolling after I added the subviews. The solution for this problem was in the xib for the view in file inspector Use Autolayout was checked. I unchecked it and the scroll view scrolled after adding the subviews.

The solution was uncheking the Use Autolayout in file inspector in xib.