在UIScrollView中執行多個UIViewControllers,我手動初始化了UIViewController,並且將它們作為子視圖添加到Scrollview,至此都一切正常。但是scrollview滾動只能顯示一個UIViewController,再多一個也不行。
怎麼會這樣?請幫忙找一下原因
DTArticle *article = [self.articles objectAtIndex:0];
DTArticle *article2 = [self.articles objectAtIndex:1];
UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
DTArticleViewController *controller = [mainStoryBoard instantiateViewControllerWithIdentifier:@"DTArticleViewController"];
DTArticleViewController *controller2 = [mainStoryBoard instantiateViewControllerWithIdentifier:@"DTArticleViewController"];
controller.article = article;
controller2.article = article2;
[self.parentScrollView addSubview:controller.view];
[self.parentScrollView addSubview:controller2.view];
self.parentScrollView.contentSize = CGSizeMake(self.parentScrollView.frame.size.width
* 2, self.parentScrollView.frame.size.height);
self.parentScrollView.showsHorizontalScrollIndicator = YES;
[self.parentScrollView setPagingEnabled:YES];
[self.view addSubview:self.parentScrollView];
在將兩個viewController的view添加到srcollview中時,需要設置這兩個控制器的view在uiscrollview中的顯示的位置,說白了,就是在addsubview之前,先將兩個控制器的view的frame重新設置,指定。
像你上面的代碼,肯定只會顯示一個控制器的view.原因是另一個視圖會被最後添加的那個視圖覆蓋住。