UIPageViewController将视图控制器设置在屏幕外

时间:2015-05-06 23:42:33

标签: ios objective-c uipageviewcontroller

我正在尝试使用以下代码将UIPageViewController添加到我的应用程序:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *viewControllers = @[page0,
                        page1,
                        page2];

    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll
                                                          navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal
                                                                        options:nil];
    self.pageController.dataSource = self;
    self.pageController.view.frame = self.view.frame;
    [self.pageController setViewControllers:@[[viewControllers firstObject]]
                                  direction:UIPageViewControllerNavigationDirectionForward
                                   animated:NO
                                 completion:nil];
    [self.pageController willMoveToParentViewController:self];
    [self addChildViewController:self.pageController];
    [self.view addSubview:self.pageController.view];
    [self.pageController didMoveToParentViewController:self];
}

然后问题是当我运行应用程序时,使用模拟器或物理设备,我得到一个空屏幕。如果我尝试滑动视图,我可以看到我实际添加到viewControllers数组的页面已经存在,但在屏幕之外。使用SparkInspector进行一些调试,我可以看到视图实际上存在,但是具有相同值的偏移量等于屏幕宽度。 我还为UIPageViewController实现了DataSource。以下是委派方法的实现:

- (NSInteger) presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    return 3;
}

- (NSInteger) presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    return 0;
}

- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {

    if (viewController == nil) {
        return [viewControllers firstObject];
    }

    NSUInteger index = [viewControllers indexOfObject:viewController];
//    NSParameterAssert(index != NSNotFound);

    index++;

    if (index == viewControllers.count) {
        return nil;
    }

    return [viewControllers objectAtIndex:index];
}

- (UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {

    if (viewController == nil) {
        return [viewControllers firstObject];
    }

    NSUInteger index = [viewControllers indexOfObject:viewController];

    if (index == 0) {
        return nil;
    }
    index--;
    return [viewControllers objectAtIndex:index];
}

我无法理解为什么App会偏移视图控制器,因此任何建议都会非常有用。

0 个答案:

没有答案