How to implement UIPageViewController with a UIScrollView?

前端 未结 1 1525
轮回少年
轮回少年 2021-01-23 06:54

I took the Photo Scroller example from Apple\'s website and tried to implement my own Album by copying the code. Now, the UIScrollView is not visible. How do I make

1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-23 07:53

    You are not adding the pageViewController's view as a subview of BasePhotoViewController's view. Your BasePhotoViewController class should look something like this. Note the code in viewDidLoad.

    BasePhotoViewController.h:

    @interface BasePhotoViewController : UIViewController 
    @property (nonatomic, strong) UIPageViewController * pageViewController;
    @end
    

    BasePhotoViewController.m:

    #import "BasePhotoViewController.h"
    #import "PhotoViewController.h"
    
    @implementation BasePhotoViewController
    
    @synthesize pageViewController;
    
    - (id)initWithCoder:(NSCoder *)coder
    {
        self = [super initWithCoder:coder];
        if (self) {
            PhotoViewController *pageZero = [PhotoViewController photoViewControllerForPageIndex:0];
            if (pageZero != nil)
            {
                self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:0
                                                                          navigationOrientation:0
                                                                                        options:nil];
                self.pageViewController.dataSource = self;
    
                [self.pageViewController setViewControllers:@[pageZero]
                                                  direction:UIPageViewControllerNavigationDirectionForward
                                                   animated:NO
                                                 completion:NULL];
            }
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        [self addChildViewController:self.pageViewController];
        [self.view addSubview:self.pageViewController.view];
        [self.pageViewController didMoveToParentViewController:self];
    }
    
    # pragma mark - UIPageViewControllerDataSource
    
    - (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerBeforeViewController:(PhotoViewController *)vc
    {
        NSUInteger index = vc.pageIndex;
        return [PhotoViewController photoViewControllerForPageIndex:(index - 1)];
    }
    
    - (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerAfterViewController:(PhotoViewController *)vc
    {
        NSUInteger index = vc.pageIndex;
        return [PhotoViewController photoViewControllerForPageIndex:(index + 1)];
    }
    
    @end
    

    Note: I've initialised the UIPageViewController in initWithCoder: because Apple's Photo Scroller code uses a storyboard. I removed the UIPageViewController from the storyboard and created a BasePhotoViewController in its place. If you are not loading BasePhotoViewController from a storyboard, you should move the code in initWithCoder: to the appropriate initialiser.

    EDIT: See this sample project on github.

    0 讨论(0)
提交回复
热议问题