【问题标题】:Rotation of UIPageViewController in Container View ControllerUIPageViewController 在容器视图控制器中的旋转
【发布时间】:2012-04-13 08:16:29
【问题描述】:

我有一个包含其他 ViewController 的 UIViewController。初始 ViewController 在 vi​​ewDidLoad 中设置:

FirstViewController *first = [FirstViewController alloc] init];
first.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
first.view.frame = m_content.frame;

[self addChildViewController:first];
[m_content.view addSubview:first.view];
[first didMoveToParentViewController:self];
m_activeViewController = first;

这个容器控制器实现了自动ForwardAppearanceAndRotationMethodsToChildViewControllers返回YES。它还实现了对非活动视图控制器的手动正向旋转更改

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    for(UIViewController *vc in m_viewControllers)
    {
        if(vc != [m_activeViewController]
            [vc willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    }
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    for(UIViewController *vc in m_viewControllers)
    {
        if(vc != [m_activeViewController]
            [vc didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    }
}

当点击菜单按钮时,我会在 ViewControllers 之间进行转换。

- (void)onMenuItemTapped:(id)sender
{
    UIViewController *vc = [m_viewControllers objectAtIndex:sender.tag];

    vc.view.frame = m_content.frame;
    [self addChildViewController:vc];
    [self transitionFromViewController:m_activeViewController toViewController:vc duration:0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) {
    [vc didMoveToParentViewController:self];
    [m_activeViewController removeFromParentViewController];
    m_activeViewController = vc;
    }];
}

这种过渡适用于我的“普通”视图控制器,并且它们在方向更改后正确显示,即使它们未处于活动状态。但是,其中一个称为 SecondCV 的子视图控制器具有 UIPageViewController 作为子视图控制器。我将 UIPageViewControllerDelegateUIPageViewControllerDataSource 设置为此 SecondCV 并在 pageViewController:spineLocationForInterfaceOrientation: 中返回 UIPageViewControllerSpineLocationMin 用于纵向和 UIPageViewControllerSpineLocationMid 用于横向。此 SecondVC 的旋转在其处于活动状态时可以正常工作 - 有两个页面在横向模式下,一个在纵向模式下正确显示。但是当这个 SecondVC 不活动时,旋转是不正确的。即使 pageViewController:spineLocationForInterfaceOrientation: 被调用,纵向和横向模式仍然有一个页面。我正在尝试解决此问题一段时间,但我没有看到任何其他选项。你有任何想法如何解决这个问题吗?

谢谢。

【问题讨论】:

    标签: containers uipageviewcontroller screen-rotation


    【解决方案1】:

    我已经解决了这种棘手的问题。

    实际上,SecondVC 已正确旋转,或者至少,脊椎位置已正确更改。因此,当我旋转设备并且 SecondVC 在父视图中不是活动视图控制器时,它 HAS 收到了旋转消息,并且也调用了 pageViewController:spineLocationForInterfaceOrientation:。我为新界面方向设置了正确的脊椎位置,并将 setViewControllers 调用到 SecondVC 的页面视图控制器。问题是,即使我为 UIPageViewControllerSpineLocationMid 设置了两个视图控制器,在显示 SecondVC 之后仍然只有一个,就像在 UIPageViewControllerSpineLocationMin 中一样。

    我已经“修复”了这个问题,再次在 viewWillAppear: 中将视图控制器设置为页面视图控制器,因为当调用此方法时,页面视图控制器的脊椎位置是正确的并且基于此信息我设置一页或两页(视图控制器)

    - (void)viewWillAppear:(BOOL)animated
    {
    if(m_pageController.spineLocation == UIPageViewControllerSpineLocationMid)
    {
        LeftPageController *left;
        RightPageController *right;
        if(m_pageController.viewControllers.count > 0)
        {
            left = [m_pageController.viewControllers objectAtIndex:0];
    
            right = [m_storyboard instantiateViewControllerWithIdentifier:@"rightPage"];
            [right loadView];
        }
    
        [m_pageController setViewControllers:[NSArray arrayWithObjects:left, right, nil] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
    
        [m_left hideGallery];
    }
    
    if(m_pageController.spineLocation == UIPageViewControllerSpineLocationMin)
    {
    
        [m_pageController setViewControllers:[NSArray arrayWithObject:[m_pageController.viewControllers objectAtIndex:0]] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
    }
    
    [super viewWillAppear:animated];
    }
    
    
    - (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
    {
    if(UIInterfaceOrientationIsPortrait(orientation))
    {
        LeftPageController *left = [pageViewController.viewControllers objectAtIndex:0];
        [pageViewController setViewControllers:[NSArray arrayWithObject:left] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
    
        pageViewController.doubleSided = NO;     
    
        return UIPageViewControllerSpineLocationMin;
    }
    
    if(UIInterfaceOrientationIsLandscape(orientation))
    {    
        LeftPageController *left;
        RightPageController *right;
        if(pageViewController.viewControllers.count > 0)
        {
            left = [pageViewController.viewControllers objectAtIndex:0];
            right = [m_storyboard instantiateViewControllerWithIdentifier:@"rightPage"];
            [right loadView];
        }
    
        [pageViewController setViewControllers:[NSArray arrayWithObjects:left,right, nil] direction:UIPageViewControllerNavigationDirectionForward | UIPageViewControllerNavigationDirectionReverse animated:NO completion:nil];
    
        return UIPageViewControllerSpineLocationMid;
    }
    
    return UIPageViewControllerSpineLocationMin;
    }
    

    我知道我做了两次同样的事情,但这是我在这种情况下愿意付出的代价。为什么在调用 pageViewController:spineLocationForInterfaceOrientation: 并为 Landscape 设置两个视图控制器后,当 SecondVC 处于非活动状态时,激活 SecondVC 后 viewWillAppear: 中的 m_pageController 中只有一个 ViewController 仍然是一个谜我。

    【讨论】:

    • 我也在使用页面视图控制器,并且我已将 VC 添加到数组中,但不知道转换各种 VC 的正确方法,如何使用 page curl 转换移动到下一个 VC。请您指导我或发布一些示例代码。
    • link我想你可以在这里找到everythink,如果没有,联系我
    • 我还没有任何解决方案,如果你能指导我,请看看我的问题。 "stackoverflow.com/questions/13776894/…"
    【解决方案2】:

    我在 iOS 6 中加入了以下代码来解决这个问题:

    - (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
    {
    
        NSArray *allPageControllers = pageViewController.viewControllers
    
        if (allPageControllers != nil) {
           [self setViewControllers:allPageControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
        }
    
        else {
    
           UIViewController *defaultViewController = [[UIViewController alloc] init];
           [self setViewControllers:[NSArray arrayWithObject:defaultViewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
           [defaultViewController release];
        }
    
        return UIPageViewControllerSpineLocationMin;
    }
    

    希望它对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多