首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ios5 -在横向视图中时在页面视图控制器中翻页

ios5 -在横向视图中时在页面视图控制器中翻页
EN

Stack Overflow用户
提问于 2012-01-24 09:35:19
回答 1查看 4.6K关注 0票数 1

我在我的应用程序中使用了UIPageViewController和故事板。

在画像中,我使用下面的代码从一个页面跳转到另一个页面,它工作得很好。

代码语言:javascript
运行
复制
int direction = UIPageViewControllerNavigationDirectionForward;
if ([self.modelController currentPage] < pagenum)
{
    direction = UIPageViewControllerNavigationDirectionForward;
}
else if ([self.modelController currentPage] > pagenum)
{
    direction = UIPageViewControllerNavigationDirectionReverse;
}

[self.pageViewController setViewControllers:[NSArray arrayWithObject:[self.modelController viewControllerAtIndex:pagenum storyboard:self.storyboard]] direction:direction animated:YES completion:NULL];    

但是,当我们处于横向模式时,相同的代码就不起作用了。在风景中如何翻页?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-06 05:59:39

如果您查看Xcode中的基于页面的应用程序模板,您会发现以下UIPageViewControllerDelegate方法:

代码语言:javascript
运行
复制
- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    if (UIInterfaceOrientationIsPortrait(orientation)) {
        // In portrait orientation: Set the spine position to "min" and the page view controller's view controllers array to contain just one view controller. Setting the spine position to 'UIPageViewControllerSpineLocationMid' in landscape orientation sets the doubleSided property to YES, so set it to NO here.
        UIViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
        NSArray *viewControllers = [NSArray arrayWithObject:currentViewController];
        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];

        self.pageViewController.doubleSided = NO;
        return UIPageViewControllerSpineLocationMin;
    }

    // In landscape orientation: Set set the spine location to "mid" and the page view controller's view controllers array to contain two view controllers. If the current page is even, set it to contain the current and next view controllers; if it is odd, set the array to contain the previous and current view controllers.
    DataViewController *currentViewController = [self.pageViewController.viewControllers objectAtIndex:0];
    NSArray *viewControllers = nil;

    NSUInteger indexOfCurrentViewController = [self.modelController indexOfViewController:currentViewController];
    if (indexOfCurrentViewController == 0 || indexOfCurrentViewController % 2 == 0) {
        UIViewController *nextViewController = [self.modelController pageViewController:self.pageViewController viewControllerAfterViewController:currentViewController];
        viewControllers = [NSArray arrayWithObjects:currentViewController, nextViewController, nil];
    } else {
        UIViewController *previousViewController = [self.modelController pageViewController:self.pageViewController viewControllerBeforeViewController:currentViewController];
        viewControllers = [NSArray arrayWithObjects:previousViewController, currentViewController, nil];
    }
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:NULL];


    return UIPageViewControllerSpineLocationMid;
}

当从上面的方法返回UIPageViewControllerSpineLocationMid时,您告诉UIPageViewController它应该并排显示two UIViewControllers (即两个页面)。

这里的关键是,在调用UIPageViewController的setViewControllers:direction:animated:completion:方法时,必须传递正确数量的UIViewControllers。

显示两页的

  • ?传递两个UIViewControllers。
  • 显示一页?传递一个UIViewController.

您提供的代码永远不会向UIPageViewController传递一个以上的UIViewController。

如果UIPageViewControllerSpineLocation与您传递的UIViewControllers数量不一致,它将崩溃或什么也不做。

如果你需要进一步的帮助,请告诉我。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8981050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档