2012-03-13 70 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

相關問題