2

For testing purpose I have create project where I have two scrollview. First scrollview have images that I inserted manually and in second scrollview I have list of buttons (as menu).

I have button as right and left to scroll UIScrollView programmatically.

After clicking this button I was moving scrollview by setting content size.

myCounter.text = [NSString stringWithFormat:@"%d", [myCounter.text intValue] - 1];

CGRect page = CGRectMake(320*([myCounter.text intValue] ), 230, 320, 150);

[myScrollView scrollRectToVisible:page animated:YES];

CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 20, 320, 200);

[firstScrollView scrollRectToVisible:page2 animated:YES];

The problem I am facing is only first scroll view is getting scrolled but not the second one.

I am really confused why this is happening.

I am also attaching same project at dropbox as code is little more.

Project at dropbox


Full Code if some one don't want to download project

.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;
@property (weak, nonatomic) IBOutlet UILabel *myCounter;
@property (weak, nonatomic) IBOutlet UIScrollView *firstScrollView;

- (IBAction)goToLeft:(id)sender;
- (IBAction)goToRight:(id)sender;
@end

.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize myScrollView, myCounter, firstScrollView;


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    firstScrollView.contentSize = CGSizeMake(800, 200);

    myCounter.text = @"0";

}

- (void) viewDidAppear:(BOOL)animated {
    for (int j = 0; j < 2; j++) {
        NSString *bname = @"";
        int x = 20;
        for (int i = 0; i < 8; i++) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 66, 75)];
            UIButton *rightArrowButton;


            UIButton *leftArrowButton;

            leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(x-38, 49, 26, 52)];

            if (i==2) {
                rightArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(294, 49, 26, 52)];
                leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(1, 49, 26, 52)];
            } else if (i==5) {
                rightArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(614, 49, 26, 52)];
                leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(320, 49, 26, 52)];
            } else if (i==7) {
                rightArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(934, 49, 26, 52)];
                leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(640, 49, 26, 52)];
            } else {
                rightArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(x+62, 49, 26, 52)];
                leftArrowButton = [[UIButton alloc] initWithFrame:CGRectMake(x-38, 49, 26, 52)];
            }


            bname = @"";
            if (i==0) {
                bname = @"doctors_icon.png";
                rightArrowButton.hidden = YES;
                leftArrowButton.hidden = YES;
            }
            if (i==1) {
                bname = @"all-offers_icon.png";
                rightArrowButton.hidden = YES;
                leftArrowButton.hidden = YES;
            }
            if (i==2) {
                bname = @"hospitals_icon.png";
                rightArrowButton.hidden = NO;
                leftArrowButton.hidden = YES;
            }

            if (i==3) {
                bname = @"ads_icon.png";
                rightArrowButton.hidden = YES;
                leftArrowButton.hidden = YES;
            }
            if (i==4) {
                bname = @"alternative_icon.png";
                rightArrowButton.hidden = YES;
                leftArrowButton.hidden = YES;
            }
            if (i==5) {
                bname = @"pharmacy_icon.png";
                rightArrowButton.hidden = NO;
                leftArrowButton.hidden = NO;
            }

            if (i==6) {
                bname = @"equ_icon.png";
                rightArrowButton.hidden = YES;
                leftArrowButton.hidden = YES;
            }

            if (i==7) {
                bname = @"supplies_icon.png";
                rightArrowButton.hidden = YES;
                leftArrowButton.hidden = NO;
            }

            UIImage *btnImage = [UIImage imageNamed:bname];
            [button setImage:btnImage forState:UIControlStateNormal];
            [button setTag:(i+1)];
            [myScrollView addSubview:button];


            btnImage = [UIImage imageNamed:@"r_arrow.png"];
            [rightArrowButton setImage:btnImage forState:UIControlStateNormal];
            [rightArrowButton setTag:998];
            [rightArrowButton addTarget:self action:@selector(goToRight:) forControlEvents:UIControlEventTouchUpInside];
            [myScrollView addSubview:rightArrowButton];

            btnImage = [UIImage imageNamed:@"l_arrow.png"];
            [leftArrowButton setImage:btnImage forState:UIControlStateNormal];
            [leftArrowButton setTag:999];
            [leftArrowButton addTarget:self action:@selector(goToLeft:) forControlEvents:UIControlEventTouchUpInside];
            [myScrollView addSubview:leftArrowButton];


            UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(x, 76, 66, 75)];

            if (i==0) {
                bname = @"medical_icon.png";
            }
            if (i==1) {
                bname = @"dental_icon.png";
            }
            if (i==2) {
                bname = @"beauty_icon.png";
            }
            if (i==3) {
                bname = @"labs_icon.png";
            }
            if (i==4) {
                bname = @"magazine_icon.png";
            }
            if (i==5) {
                bname = @"news_icon.png";
            }

            if (i<=5) {

                btnImage = [UIImage imageNamed:bname];
                [button2 setImage:btnImage forState:UIControlStateNormal];
                [button2 setTag:(i+8+1)];
                [myScrollView addSubview:button2];

            }

            x += button.frame.size.width + 40;

        }
    }
    //    myScrollView.delegate = self;
    myScrollView.contentSize = CGSizeMake(320*3, 150);
    myScrollView.backgroundColor = [UIColor clearColor];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)goToLeft:(id)sender {

    myCounter.text = [NSString stringWithFormat:@"%d", [myCounter.text intValue] - 1];

    CGRect page = CGRectMake(320*([myCounter.text intValue] ), 230, 320, 150);

    [myScrollView scrollRectToVisible:page animated:YES];

    CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 20, 320, 200);

    [firstScrollView scrollRectToVisible:page2 animated:YES];


    NSLog(@"myCounter.text==%d", 320*([myCounter.text intValue]));

}

- (IBAction)goToRight:(id)sender {
    myCounter.text = [NSString stringWithFormat:@"%d", [myCounter.text intValue] + 1];

    CGRect page = CGRectMake(320*([myCounter.text intValue] ), 230, 320, 150);
    [myScrollView scrollRectToVisible:page animated:YES];

    CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 20, 320, 200);
    [firstScrollView scrollRectToVisible:page2 animated:YES];
    NSLog(@"myCounter.text==%d", 320*([myCounter.text intValue]));
}
@end

2 Answers 2

1

I have downloaded your code and modify the code below, your "page.origin.y is out of the contentSize.height"

- (IBAction)goToLeft:(id)sender {

    myCounter.text = [NSString stringWithFormat:@"%d", [myCounter.text intValue] - 1];

    CGRect page = CGRectMake(320*([myCounter.text intValue] ), 0, 320, 150);

    [myScrollView scrollRectToVisible:page animated:YES];

    CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 0, 320, 200);

    [firstScrollView scrollRectToVisible:page2 animated:YES];


    NSLog(@"myCounter.text==%d", 320*([myCounter.text intValue]));

}

- (IBAction)goToRight:(id)sender {
    myCounter.text = [NSString stringWithFormat:@"%d", [myCounter.text intValue] + 1];

    CGRect page = CGRectMake(320*([myCounter.text intValue] ), 0, 320, 150);
    [myScrollView scrollRectToVisible:page animated:YES];

    CGRect page2 = CGRectMake(320*([myCounter.text intValue] ), 0, 320, 200);
    [firstScrollView scrollRectToVisible:page2 animated:YES];
    NSLog(@"myCounter.text==%d", 320*([myCounter.text intValue]));
}
5
  • this is working, but just curious, why my first scrollview code was working? Dec 11, 2013 at 8:53
  • 1
    @FahimParkar page.origin.y is out of the contentSize.height, scrollRectToVisible only scroll within contentSize I think.
    – KudoCC
    Dec 11, 2013 at 8:55
  • I understand, but CGRect page = CGRectMake(320*([myCounter.text intValue] ), 20, 320, 150); (20 instead of 0 as y position) was working with first scrollview... I think content size should be within view scope... Dec 11, 2013 at 8:57
  • @FahimParkar your contentSize.height is 150, but you set the page.origin.y to 230 , there is nothing in {320*x, 230, 320, 150}.
    – KudoCC
    Dec 11, 2013 at 9:00
  • @FahimParkar y=230 is in the Coordinate System of scrollview's parent view and page is in the Coordinate system of scrollview.
    – KudoCC
    Dec 11, 2013 at 9:06
0

Hey mate please set the contentSize of the scrollview in your viewDidLoad method.

[myScrollView setContentSize:CGSizeMake(320*3,200)];

Thanks and have a blessed day

2
  • are you serious? if you see my question I already have it... myScrollView.contentSize = CGSizeMake(320*3, 150); Dec 11, 2013 at 8:00
  • scrollview doesn't have any other reason of scrolling might be some wrong place of declaration. I have never faced any of such problems that the scrollview doesn't work. Let me see it later i need to go for the lunch. Catch you later Dec 11, 2013 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.