1
votes

My UIScrollView isn't scrolling. (I'm trying to get it to scroll horizontally) I'm setting the contentSize to be (960, 300). I have set the framesize of the UIScrollView to be width:320 height:300 in the storyboard.

I have a container view inside the scroll view that has a width of 960 points.

At 320 points, I have a subview that has a background color of brown. When I scroll, I can see the brown subview, but it bounces back when I let go of the drag.

This is my viewDidLoad method:

- (void)viewDidLoad
    [super viewDidLoad];
    self.scrollView.scrollEnabled = YES;

    [self.scrollView setFrame:CGRectMake(0,0,320,300)];
    self.scrollView.contentSize = CGSizeMake(960, 300);

    UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 300)];
    [subview1 setBackgroundColor:[UIColor brownColor]];

    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 960, 300)];
    [containerView addSubview:subview1];

    [self.scrollView addSubview:containerView];
}
2
Do you have a autolayout option enabled? - Nikita Took

2 Answers

1
votes

Here is a sample code for create a scroll view programmatically:

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
scrollView.backgroundColor = [UIColor redColor];
scrollView.scrollEnabled = YES;
scrollView.contentSize = CGSizeMake(960, 300);
[self.view addSubview:scrollView];

UIView *subview1 = [[UIView alloc] initWithFrame:CGRectMake(320, 0, 320, 300)];
[subview1 setBackgroundColor:[UIColor brownColor]];

UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 960, 300)];
containerView.backgroundColor = [UIColor greenColor];
[containerView addSubview:subview1];

[scrollView addSubview:containerView];

So, there is no issue in your code, but something wrong with your storyboard configure.

0
votes

I have a few thoughts about what you are trying to accomplish. I don't know how old is your code, but today we have many better ways to work with scrollview and they don't include setting up the intrinsic size or fame.

Well, despite my doubt about the objective of the code, I tried to run it here, using storyboard, a single view, a default configured scrollView and the experiment went well, your code actually works, I think maybe you have some problem with your scrollView. I know this will sound weird but did you checked if the scrollView has the property "Scrolling Enabled" checked?

If you can give me more information about this issue, I can help you.