UIScrollView不能在iOS 6和7中以编程方式滚动

为了testing目的,我创build了两个滚动视图的项目。 第一个滚动视图有手动插入的图像,在第二个滚动视图中我有button列表(作为菜单)。

我有button左右滚动UIScrollView编程。

点击这个button后,我通过设置内容大小来移动滚动视图。

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]; 

我面临的问题是只有第一个滚动视图正在滚动,而不是第二个。

我真的很困惑,为什么发生这种情况。

我也是在Dropbox上附加同样的项目,因为代码是多一点。

Dropbox项目


完整的代码,如果有人不想下载项目

。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 

我已经下载了你的代码并修改了下面的代码,你的“page.origin.y不在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])); } 

嘿伙伴请在viewDidLoad方法中设置scrollview的contentSize。

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

谢谢你,有一个幸运的一天