UIScrollView 的 ContentView 中的 UIViewControllers?

Posted

技术标签:

【中文标题】UIScrollView 的 ContentView 中的 UIViewControllers?【英文标题】:UIViewControllers in ContentView of UIScrollView? 【发布时间】:2016-03-07 11:29:08 【问题描述】:

我不想使用页面控制,因为当用户水平滚动时我必须更改按钮。

所以我使用的是 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];

故事板的层次结构是

【问题讨论】:

【参考方案1】:

从视图层次结构中删除容器视图,您的滚动将起作用。

并从以下代码中复制您的 viewDidLoad() 方法。

- (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]];
//    


【讨论】:

Container View 必不可少 1. UITableView/UICollection 中的动态高度 2. 会调用添加的ViewsController的生命周期方法。 那你为什么不使用 Collection 视图而不是 Container 视图? CollectionView 不能代替 Container View。它在 UIViewController 内部,它将在 ContainerView 中显示。 TableView 的高度会自动成为 Container View 的高度。 是的,我同意,但您可以使用 UICollection 动态调整 uicollectionview 单元格大小 单元格大小只会增加单元格的高度和整个 CollectionView,但它仍然会滚动。我不想在集合视图中滚动,无论全高,它都会变成 UIContainerView 的高度。【参考方案2】:

请点击以下链接。这将解决您的问题。另外请不要写太多代码,用storyboard就能轻松搞定。

https://github.com/mluton/EmbeddedSwapping

【讨论】:

请考虑更详细的答案 =) 我有一个容器视图,需要在滚动时添加和删除视图控制器,因此无法使用 segue 并务实地添加视图控制器 @Saood。请在推荐此第 3 方之前正确阅读问题。

以上是关于UIScrollView 的 ContentView 中的 UIViewControllers?的主要内容,如果未能解决你的问题,请参考以下文章

AppDelegate 和 ContentView() 之间的 SwiftUI 通信

UIScrollView里面的UIScrollView:滚动

UIScrollView 内的 UIScrollView 时滚动

来自另一个视图控件的模态视图信息

UIScrollView 嵌入其他 UIScrollView

自定义控件中的 Xamarin BindableProperty