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