我可以添加多个滚动视图,其背景颜色已更改并且工作正常。所以基本上对于每个视图你有不同的背景颜色。但是,当我尝试为每个视图添加单独的视图时,它会将视图添加到另一视图的顶部,我不知道为什么。滚动视图在彼此顶部添加视图
UIViewController *view1 = [self.storyboard instantiateViewControllerWithIdentifier:@"View1"];
UIViewController *view2 = [self.storyboard instantiateViewControllerWithIdentifier:@"View2"];
UIViewController *view3 = [self.storyboard instantiateViewControllerWithIdentifier:@"View3"];
for (NSInteger i=0; i<ScrollViewControllerNumberOfPages; i++) {
CGFloat yOrigin = i * self.view.frame.size.height;
CGRect subViewFrame = CGRectMake(yOrigin, 0, pageHeight, pageWidth);
NSLog(@"Printing Width and Height %f %f",pageWidth,pageHeight);
UIView *subView = [[UIView alloc] initWithFrame:subViewFrame];
// Pick a random colour for the view
CGFloat randomRed = ((CGFloat)(arc4random() % 1000))/1000;
CGFloat randomGreen = ((CGFloat)(arc4random() % 1000))/1000;
CGFloat randomBlue = ((CGFloat)(arc4random() % 1000))/1000;
subView.backgroundColor = [UIColor colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1];
NSLog(@"printin i %i", i);
if (i == 0)
subView = view1.view;
else if (i == 1)
subView = view2.view;
[self.scrollView addSubview:subView];
}
我根本不知道什么是你想做,但是当你做'subView = view1.view;'你做'subView'指针指向从故事板加载的视图。也许你打算以相反的方式来做,比如'view1.view = subView'? – lawicko 2012-03-13 16:48:38
不需要。我想在滚动视图中一个接一个地添加三个不同的视图 – CodeGeek123 2012-03-13 16:58:29
for循环中前10行左右的代码看起来毫无意义,因为您之后将subView设置为视图控制器的视图? – wattson12 2012-03-13 17:00:51