2016-03-07 55 views
0

我不想使用頁面控制,因爲我必須在用戶水平滾動時更改按鈕。UIScrollView的ContentView中的UIViewControllers?

所以我使用UIScrollview和容器視圖。

通過以下this tutorial 我能夠在容器視圖中添加子視圖控制器,但滾動視圖不滾動自動佈局。

這裏是我的代碼

- (void)viewDidLoad { 
[super viewDidLoad]; 

self.array_pageContent = [[NSMutableArray alloc] init]; 

CallViewController *objGameReviewPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CallViewController"]; 
[self addChildViewController:objGameReviewPageContentViewController]; 
UIView * view = objGameReviewPageContentViewController.view; 




HomeViewController *objHomeViewController= [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"]; 
[self addChildViewController:objHomeViewController]; 
UIView * view1 = objHomeViewController.view; 





GroupViewController *objGroupViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"GroupViewController"]; 
[self addChildViewController:objGroupViewController]; 
UIView * view2 = objGroupViewController.view; 




CallenderViewController *objCallenderViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CallenderViewController"]; 
[self addChildViewController:objCallenderViewController]; 
UIView * view3 = objGroupViewController.view; 




CasesViewController *objCasesViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CasesViewController"]; 
[self addChildViewController:objCasesViewController]; 
UIView * view4 = objGroupViewController.view; 




[self.scrollViewContent setPagingEnabled:YES]; 
[self.scrollViewContent setScrollEnabled:YES]; 
[self.scrollViewContent setShowsHorizontalScrollIndicator:YES]; 
[self.scrollViewContent setShowsVerticalScrollIndicator:NO]; 
[self.scrollViewContent setDelegate:self]; 




    [self.array_pageContent addObject:view]; 
    [self.array_pageContent addObject:view1]; 
    [self.array_pageContent addObject:view2]; 
    [self.array_pageContent addObject:view3]; 
    [self.array_pageContent addObject:view4]; 


NSInteger pageCount = self.array_pageContent.count; 

self.pageControl.currentPage = 0; 
self.pageControl.numberOfPages = pageCount; 

self.mutableArray_pageContentViews = [[NSMutableArray alloc] init]; 
for (NSInteger i = 0; i < pageCount; ++i) { 
    [self.mutableArray_pageContentViews addObject:[NSNull null]]; 
} 

} 

- (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 

CGSize pagesScrollViewSize = self.scrollViewContent.frame.size; 
self.scrollViewContent.contentSize = CGSizeMake(pagesScrollViewSize.width * self.array_pageContent.count, pagesScrollViewSize.height); 

[self loadVisiblePages]; 
} 

- (void)loadPage:(NSInteger)page { 
if (page < 0 || page >= self.array_pageContent.count) { 
    return; 
} 



UIView *pageView = [self.mutableArray_pageContentViews objectAtIndex:page]; 
if ((NSNull*)pageView == [NSNull null]) { 
    CGRect frame = self.scrollViewContent.frame; 
    frame.origin.x = frame.size.width * page; 
    frame.origin.y = 0.0f; 

    UIView *newPageView = nil; 
    newPageView = [self.array_pageContent objectAtIndex:page]; 

    newPageView.frame = frame; 
    [self.scrollViewContent addSubview:newPageView]; 
    [self.mutableArray_pageContentViews replaceObjectAtIndex:page withObject:newPageView]; 
} 
} 

- (void)purgePage:(NSInteger)page { 
if (page < 0 || page >= self.array_pageContent.count) { 
    return; 
} 

UIView *pageView = [self.mutableArray_pageContentViews objectAtIndex:page]; 
if ((NSNull*)pageView != [NSNull null]) { 
    [pageView removeFromSuperview]; 
    [self.mutableArray_pageContentViews replaceObjectAtIndex:page withObject:[NSNull null]]; 
} 
} 

- (void)loadVisiblePages { 
CGFloat pageWidth = self.scrollViewContent.frame.size.width; 
NSInteger page = (NSInteger)floor((self.scrollViewContent.contentOffset.x * 2.0f + pageWidth)/(pageWidth * 2.0f)); 

self.pageControl.currentPage = page; 

NSInteger firstPage = page - 1; 
NSInteger lastPage = page + 1; 

for (NSInteger i=0; i<firstPage; i++) { 
    [self purgePage:i]; 
} 

for (NSInteger i=firstPage; i<=lastPage; i++) { 
    [self loadPage:i]; 
} 

for (NSInteger i=lastPage+1; i<self.array_pageContent.count; i++) { 
    [self purgePage:i]; 
} 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
[self loadVisiblePages]; 
} 

而對於故事板的層次是 enter image description here

回答

2

取出容器視圖從視圖層次中的滾動會工作。

和replcae您的viewDidLoad()metod從下面的代碼。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    CGFloat width = 0.0; 

    self.array_pageContent = [[NSMutableArray alloc] init]; 

    CallViewController *objGameReviewPageContentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CallViewController"]; 
    [self addChildViewController:objGameReviewPageContentViewController]; 
    width = objGameReviewPageContentViewController.view.frame.size.width; 
    [self.scrollViewContent addSubview:objGameReviewPageContentViewController.view]; 

// UIView * view = objGameReviewPageContentViewController.view; 




    HomeViewController *objHomeViewController= [self.storyboard instantiateViewControllerWithIdentifier:@"HomeViewController"]; 

    CGRect frame = objHomeViewController.view.frame; 
    frame.origin.x = objGameReviewPageContentViewController.view.frame.size.width; 
    objHomeViewController.view.frame = frame; 
    width += frame.size.width; 

    NSLog(@"%f",width); 

    [self addChildViewController:objHomeViewController]; 
    [self.scrollViewContent addSubview:objHomeViewController.view]; 
// UIView * view1 = objHomeViewController.view; 





    GroupViewController *objGroupViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"GroupViewController"]; 

    frame = objGroupViewController.view.frame; 
    frame.origin.x = objGameReviewPageContentViewController.view.frame.size.width*2; 
    objGroupViewController.view.frame = frame; 
    width += frame.size.width; 

    NSLog(@"%f",width); 

    [self addChildViewController:objGroupViewController]; 
    [self.scrollViewContent addSubview:objGroupViewController.view]; 
// [self addChildViewController:objGroupViewController]; 
// UIView * view2 = objGroupViewController.view; 




    CallenderViewController *objCallenderViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CallenderViewController"]; 

    frame = objCallenderViewController.view.frame; 
    frame.origin.x = objGameReviewPageContentViewController.view.frame.size.width*3; 
    objCallenderViewController.view.frame = frame; 
    width += frame.size.width; 

    NSLog(@"%f",width); 

    [self addChildViewController:objCallenderViewController]; 
    [self.scrollViewContent addSubview:objCallenderViewController.view]; 

// [self addChildViewController:objCallenderViewController]; 
// UIView * view3 = objGroupViewController.view; 




    CasesViewController *objCasesViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"CasesViewController"]; 

    frame = objCasesViewController.view.frame; 
    frame.origin.x = objGameReviewPageContentViewController.view.frame.size.width*4; 
    objCasesViewController.view.frame = frame; 
    width += frame.size.width; 

    NSLog(@"%f",width); 

    [self addChildViewController:objCasesViewController]; 
    [self.scrollViewContent addSubview:objCasesViewController.view]; 

// [self addChildViewController:objCasesViewController]; 
// UIView * view4 = objGroupViewController.view; 




    [self.scrollViewContent setPagingEnabled:YES]; 
    [self.scrollViewContent setScrollEnabled:YES]; 
    [self.scrollViewContent setShowsHorizontalScrollIndicator:YES]; 
    [self.scrollViewContent setShowsVerticalScrollIndicator:NO]; 
    [self.scrollViewContent setDelegate:self]; 

    self.scrollViewContent.contentSize = CGSizeMake(width, self.scrollViewContent.frame.size.height); 




//  [self.array_pageContent addObject:view]; 
//  [self.array_pageContent addObject:view1]; 
//  [self.array_pageContent addObject:view2]; 
//  [self.array_pageContent addObject:view3]; 
//  [self.array_pageContent addObject:view4]; 


// NSInteger pageCount = self.array_pageContent.count; 
//  
// self.pageControl.currentPage = 0; 
// self.pageControl.numberOfPages = pageCount; 
//  
// self.mutableArray_pageContentViews = [[NSMutableArray alloc] init]; 
// for (NSInteger i = 0; i < pageCount; ++i) { 
//  [self.mutableArray_pageContentViews addObject:[NSNull null]]; 
// } 

} 
+0

容器視圖是必不可少的 1. UITableView/UICollection中的動態高度 2.它將調用添加的ViewsControllers的生命週期方法。 –

+0

那麼爲什麼你沒有使用集合視圖,而是容器視圖? –

+0

CollectionView不是Container視圖。它的內部UIViewController將在ContainerView中顯示。 TableView的高度將自動成爲Container View的高度。 –

0

請按下面的鏈接。這將解決您的問題。也請不要編寫太多的代碼,這將很容易與故事板完成。

https://github.com/mluton/EmbeddedSwapping

+0

考慮更詳細的回答,請=) – Paradoxetion

+0

我有一個容器視圖,並需要添加和刪除視圖控制器,當我滾動,不能使用SEGUE務實添加視圖控制器 –

+0

@Saood。請在建議此第三方之前正確閱讀問題。 –

相關問題