UIPageViewController转换PageCurl与UIScrollView错误的页面索引

时间:2014-11-22 13:18:41

标签: ios objective-c uiwebview uiscrollview uipageviewcontroller

在发布之前我搜索了很多但没有成功:

我有UIPageViewController数组UIViewController和PageCurl作为转换。

每个UIViewController都由UIScrollViewUIWebView组成。

加载内容后根据网页浏览量高度计算的滚动高度。

现在当我加载UIPageViewController并在UIViewController之间开始导航而不滚动内容时,每件事情都可以正常工作。但是,如果我滚动任何页面,然后导航到下一页或上一页。它将重新显示同一页面,然后再正常工作。

我在IOS7和IOS8上测试了它:

UIPageViewController viewDidLoad方法:

    self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];

    [self.pageController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

self.pageController.doubleSided = NO;

[self addChildViewController:self.pageController];
[[self view] addSubview:[self.pageController view]];
[self.pageController didMoveToParentViewController:self];

查看控制器webViewDidFinishLoad:

CGSize contentSize = aWebView.scrollView.contentSize;
webView.frame = CGRectMake(0, 312, contentSize.width, contentSize.height);
scrollView.contentSize = CGSizeMake(320.f, contentSize.height+60);

修改  以下是UIPageViewController的 viewControllerBeforeViewController viewControllerAfterViewController 的完整代码

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

    NSUInteger index = [(NewsDetailVC *)viewController index];
    self.index = index;
    NSLog(@"view before %i", index);
    if (index <= 0) {
        return nil;
    }

    index--;
    currentViewController = [self viewControllerAtIndex:index];
    return [self viewControllerAtIndex:index];

}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [(NewsDetailVC *)viewController index];
    NSLog(@"view after %i", index);

    if (index >= self.newsList.count -1) {
        return nil;
    }
    index++;
    currentViewController =[self viewControllerAtIndex:index];
    return [self viewControllerAtIndex:index];
}

EDIT2 上一页是一个UIViewController包含UITableView,当单击一个单元格时会将Items数组传递给UIPageViewController和所选索引。 currentViewController的目的,因为我有字体大小和当前视图背景的控件。 UIPageViewController父级是UIViewController 这是viewControllerAtIndex:

的代码
- (NewsDetailVC *)viewControllerAtIndex:(NSUInteger)index {

    NewsDetailVC *childViewController = [[UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"NewsDetailVC"];
    childViewController.index = index;
    childViewController.selectedNews = [self.newsList objectAtIndex:index ];
    childViewController.selectedCategory = self.selectedCategory;
    return childViewController;
}

1 个答案:

答案 0 :(得分:1)

我遇到了与UIPageViewController相同的问题。

似乎 viewControllerAtIndex 中显示的索引不是很准确。

正如上面评论中提到的那样,在这些情况下,您应该实现委托方法 pageViewController:willTransitionToViewControllers

在这种方法中你应该做你的工作:

- (void)pageViewController:(UIPageViewController *)pageViewController
willTransitionToViewControllers:(NSArray *)pendingViewControllers {

    if ([pendingViewControllers count] > 0) {
        NSUInteger index = [(PageContentViewController *)
        [pendingViewControllers objectAtIndex:0] pageIndex];

    if (index == [yourArray count] - 1 || index == 0) {
      [self loadContent];
    } else {
      [self doOtherStuff];
    }
  }
}

不知何故,* viewControllerAtIndex *被多次调用,给你找到索引的麻烦,但在这种方法中索引应该是准确的。

希望它有所帮助!