6

我有一个包含其他 ViewControllers 的 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:,Portrait 和 Landscape 模式下仍然有一个页面。我正在尝试解决此问题一段时间,但我没有看到任何其他选项。你有任何想法如何解决这个问题吗?

谢谢你。

4

2 回答 2

1

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

实际上,SecondVC 已正确旋转,或者至少,脊椎位置已正确更改。因此,当我旋转设备并且 SecondVC 在父视图中不是活动视图控制器时,它已收到旋转消息并且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 处于非活动状态时,viewWillAppear:中的 m_pageController 中只有一个 ViewController:激活 SecondVC 后对我来说仍然是一个谜。

于 2012-10-25T16:44:49.343 回答
0

我在 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;
}

希望它可以帮助某人!

于 2013-03-15T12:04:32.610 回答