pasawaya
pasawaya

Reputation: 11595

UIScrollView pages in the negative direction

So I have a UIScrollView set up the following way and for some reason, the scroll view is displayed like this:

                  Phone Screen

                ---------------
---------------|---------------|
---------------|---------------|
---------------|---------------|
---------------|----Content----|
---------------|---------------|
---------------|---------------|
---------------|---------------|
---------------|---------------|
                ---------------

My goal is to have the user scroll to the right, like below:

  Phone Screen

 ---------------
|---------------|---------------
|---------------|---------------
|---------------|---------------
|----Content----|---------------
|---------------|---------------
|---------------|---------------
|---------------|---------------
|---------------|---------------
 ---------------

Here's how the scroll view is set up (this is a bit simplified, but accurate):

CGFrame frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
CGSize contentSize = CGSizeMake(self.bounds.size.width*2, self.bounds.size.height);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
scrollView.contentSize = contentSize;
scrollView.propertyN = bool; //E.g. pagingEnabled, bounces, clipsToBounds, etc.

UIImageView *imageView = [[UIImageView alloc] initWithImage:someImage];
imageView.frame = frame;

[scrollView addSubview:imageView];
[self.view addSubview:scrollView];

Has anyone else had this problem? I'm sure I'm making some extremely basic mistake in my setup of the scroll view but can't seem to spot it.

Upvotes: 0

Views: 790

Answers (1)

Not Randy Marsh
Not Randy Marsh

Reputation: 414

Could the the origin for self.view.frame be offscreen (e.g. -self.bounds.width)?

I'd try starting with simple code and building back up to what you want. Here's some simple code that's similar to the code above that you can throw in a blank project to convince yourself that the code you posted isn't the likely culprit:

  CGFloat width = self.view.bounds.size.width;
  CGFloat height = self.view.bounds.size.height;
  CGRect frame = CGRectMake(0.0f,0.0f,width,height);
  CGSize contentSize = CGSizeMake(width*2, height);

  UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
  scrollView.backgroundColor = [UIColor yellowColor];
  scrollView.contentSize = contentSize;
  scrollView.pagingEnabled = YES;

  UIView *view1 = [[UIView alloc] initWithFrame:frame];
  view1.backgroundColor = [UIColor blueColor];
  UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(width,0.0f,width,height)];
  view2.backgroundColor = [UIColor greenColor];

  [scrollView addSubview:view1];
  [scrollView addSubview:view2];
  [self.view addSubview:scrollView];

Upvotes: 3

Related Questions