ios - 如何使用 UIScrollView 实现 UIPageViewController?

标签 ios objective-c uiscrollview uipageviewcontroller

我拿了Photo Scroller来自苹果网站的示例,并尝试通过复制代码来实现我自己的专辑。现在,UIScrollView 不可见。我该如何让它出现?

我所做的唯一代码更改是创建 UIPageViewController。就我而言,打开它的是 UIViewController,而不是 AppDelegate

@implementation BasePhotoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nil bundle:nibBundleOrNil];
    if (self) {
        // kick things off by making the first page

        PhotoViewController *pageZero = [PhotoViewController photoViewControllerForPageIndex:0];
        if (pageZero != nil)
        {
            // assign the first page to the pageViewController (our rootViewController)
            //UIPageViewController *pageViewController = (UIPageViewController *)    [[UIApplication sharedApplication] keyWindow].rootViewController;
            UIPageViewController *pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:0 navigationOrientation:0 options:nil];
            //UIPageViewController *pageViewController = (UIPageViewController *)self.parentViewController;
            pageViewController.dataSource = self;

            [pageViewController setViewControllers:@[pageZero]
                                     direction:UIPageViewControllerNavigationDirectionForward
                                      animated:NO
                                    completion:NULL];
        }
    }
    return self;
}

最佳答案

您没有将 pageViewController 的 View 添加为 BasePhotoViewController View 的 subview 。您的 BasePhotoViewController 类应该如下所示。请注意viewDidLoad中的代码。

BasePhotoViewController.h:

@interface BasePhotoViewController : UIViewController <UIPageViewControllerDataSource>
@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

注意:我已经在 initWithCoder: 中初始化了 UIPageViewController,因为 Apple 的 Photo Scroller代码使用 Storyboard。我从 Storyboard中删除了 UIPageViewController 并在其位置创建了一个 BasePhotoViewController。如果您没有从 Storyboard加载 BasePhotoViewController,则应将 initWithCoder: 中的代码移动到适当的初始化程序。

编辑:请参阅此 sample project在 github 上。

关于ios - 如何使用 UIScrollView 实现 UIPageViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17171777/

相关文章:

ios - 错误 : Use of undeclared type 'SCNView'

objective-c - 在 Objective-C cocoa 中使用方法

ios - 启用分页时将水平滚动 Collection View 的单元格居中,并使其他单元格部分可见

ios - UIScrollView - 无需快照即可恢复到起始位置

objective-c - 当数据为空时使用 Storyboard 在导航 Controller 上显示不同的 View

iphone - 应用程序在后台时计划的 NSTimer?

ios - swift 1.2 上的 RACCommand 编译失败

android - Firebase 数据库 - 如何执行前缀通配符查询

ios - iOS 9 中的 POST 值获取错误(NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9813)

ios - iOS 中 ScrollView 中的可扩展 UIView