我可以添加多個滾動視圖,其背景顏色已更改並且工作正常。所以基本上對於每個視圖你有不同的背景顏色。但是,當我嘗試爲每個視圖添加單獨的視圖時,它會將視圖添加到另一視圖的頂部,我不知道爲什麼。滾動視圖在彼此頂部添加視圖
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