Raj
Raj

Reputation: 71

nested scrollview in iphone

I am new to iphone development . Can anyone please tell me how to add a vertical scrollview inside a horizontal scroll view. I went through many samples but couldn't get a clear picture about it . I wan my view to be scrolled in both vertical and horizontal directions. Any help is appreciated.

Here is my code for scrolling:

EDIT: Here is my code for scrolling

  self.firstScroll.pagingEnabled=YES;

 self.firstScroll.clipsToBounds=YES;

   int numberOfViews=3;

 for(int i=0;i<numberOfViews;i++){

    CGFloat xOrigin=self.view.frame.size.width*i;

    UIView *awesomeView=[[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0,        self.view.frame.size.width, self.view.frame.size.height)];
    awesomeView.backgroundColor=[UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
    [self.firstScroll addSubview:awesomeView];
 }
self.firstScroll.contentSize=CGSizeMake(self.view.frame.size.width*numberOfViews,     self.view.frame.size.height);
[self.view addSubview:firstScroll];


self.nextVerticalScroll.pagingEnabled=YES;
for(int i=0;i<numberOfViews ;i++){
    CGFloat yOrigin=self.view.frame.size.height * i;
    UIView *verticalView=[[UIView alloc]initWithFrame:CGRectMake(0, yOrigin, self.view.frame.size.width, self.view.frame.size.height)];
  verticalView.backgroundColor=[UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
    [self.nextVerticalScroll addSubview:verticalView];
}
self.nextVerticalScroll.contentSize=CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*numberOfViews);
[self.view addSubview:nextVerticalScroll];

Thanks,

Raj

Upvotes: 0

Views: 606

Answers (2)

Namrata Sen
Namrata Sen

Reputation: 41

Please if you have added Paging in XIB then remove it...I think it will help you.

Upvotes: 0

Rob
Rob

Reputation: 437632

You probably do not want to have scrollviews within scrollviews because you can't control which gets the horizontal gestures and which gets the vertical gestures (without a lot of hassle, at least). It is much easier to have a single scrollview with a contentSize that is bigger horizontally and vertically than the bounds of the scrollview itself, e.g.:

- (void)configureScrollView
{
    NSInteger rows = 4;
    NSInteger cols = 5;

    CGFloat width = self.scrollView.bounds.size.width;
    CGFloat height = self.scrollView.bounds.size.height;

    // configure my scroll view itself

    self.scrollView.contentSize = CGSizeMake(width * cols, height * rows);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.backgroundColor = [UIColor darkGrayColor];

    // now let's add the labels to the scrollview

    for (NSInteger row = 0; row < rows; row++)
    {
        for (NSInteger col = 0; col < cols; col++)
        {
            // making my label just a little smaller than the scrollview's bounds so I can easily see the scrolling/paging

            CGRect frame = CGRectMake((width * col) + 20.0, (height * row) + 20.0, width - 40.0, height - 40.0); 

            // create and configure the label

            UILabel *label = [[UILabel alloc] initWithFrame:frame];
            label.text = [NSString stringWithFormat:@"%1.0f", row * cols + col + 1.0];
            label.textAlignment = UITextAlignmentCenter;
            label.backgroundColor = [UIColor lightGrayColor];

            // add it to my scrollview

            [self.scrollView addSubview:label];
        }
    }
}

I'm just filling my scroll view with 20 different text labels (and colored the background of the scrollview differently from the labels so I could see them), but it demonstrates the basic idea.

Upvotes: 1

Related Questions