美文网首页
UIPageViewController 的使用及问题处理

UIPageViewController 的使用及问题处理

作者: XPorter | 来源:发表于2016-08-19 15:44 被阅读357次

    对于 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;
    

    相关文章

      网友评论

          本文标题:UIPageViewController 的使用及问题处理

          本文链接:https://www.haomeiwen.com/subject/tqpbsttx.html