2012-03-13 112 views
-1

我可以添加多个滚动视图,其背景颜色已更改并且工作正常。所以基本上对于每个视图你有不同的背景颜色。但是,当我尝试为每个视图添加单独的视图时,它会将视图添加到另一视图的顶部,我不知道为什么。滚动视图在彼此顶部添加视图

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]; 
} 
+1

我根本不知道什么是你想做,但是当你做'subView = view1.view;'你做'subView'指针指向从故事板加载的视图。也许你打算以相反的方式来做,比如'view1.view = subView'? – lawicko 2012-03-13 16:48:38

+0

不需要。我想在滚动视图中一个接一个地添加三个不同的视图 – CodeGeek123 2012-03-13 16:58:29

+1

for循环中前10行左右的代码看起来毫无意义,因为您之后将subView设置为视图控制器的视图? – wattson12 2012-03-13 17:00:51

回答

1

这是在黑暗中拍摄的,因为你的代码需要一些认真的反思。

你正在做什么是你分配一个subview,修改它的框架和背景,然后完全无视这一点,它设置为先前已实例viewcontoller视图。

这应该工作(的方式):

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; 

    if (i == 0) 
     subView = view1.view; 
    else if (i == 1) 
     subView = view2.view; 

    subView.frame = 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 %d", i); 
    [self.scrollView addSubview:subView]; 
} 

它看起来有点更好,如果你不喜欢它:

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

    UIViewController *subViewCont = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"View%d",i+1]]; 

    UIView *subView = subViewCont.view; 

    subView.frame = 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 %d", i); 
    [self.scrollView addSubview:subView]; 
} 
+0

您的第一个代码有效。现在我要去尝试第二件。非常感谢你:) – CodeGeek123 2012-03-13 17:21:55

+0

不客气:) – 2012-03-13 17:24:37

相关问题