UIPageViewController 的使用及问题处理

对于 UIPageViewController查了好多资料,使用的几乎都是在一个ViewController创建一个UIPageViewController,然后把UIPageViewController的view 添加到 ViewController的view。其实UIPageViewController本身就是一个ViewController,为什么要多此一举呢?为什么不直接push或present出来呢?但是如果你项目中所有的ViewController都必须继承一个基类,那就没办法了!

为什么要多此一举

接下来就来试试,直接push进一个UIPageViewController
首先创建一个UIPageViewController,这个都一样

    UIViewController *vc = [self viewController];
    
    NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin]
                                                       forKey: UIPageViewControllerOptionSpineLocationKey];
     UIPageViewController *pageVC = [[UIPageViewController alloc] initWithTransitionStyle:
                                     UIPageViewControllerTransitionStyleScroll navigationOrientation:
                                     UIPageViewControllerNavigationOrientationHorizontal options:options];
    pageVC.dataSource = _dataSouce;
    pageVC.view.backgroundColor = [UIColor blueColor];
    pageVC.view.bounds=self.view.bounds;
    [pageVC setViewControllers:@[vc]
                     direction:UIPageViewControllerNavigationDirectionReverse
                      animated:YES completion:^(BOOL finished) {
        
    }];
    
//然后push进来
    [self.navigationController pushViewController:pageVC animated:YES];

这样感觉回不回更加爽呢,原本需要在ViewController 做的操作完全可以在UIPageViewController里面处理。同时也可以将dataSource 和 delegate单独抽离出来,用一个 NSObject的子类来处理,这样还能减少ViewController的代码。

但是问题来了,push进来的话,貌似第一个页面跟其他的不一样,布局往下偏移了?
仔细一想,UIPageViewController 里面包含的有 UIScrollView,那问题就解决了

pageVC.automaticallyAdjustsScrollViewInsets = NO;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容