6

他のViewControllerを含むUIViewControllerがあります。最初の ViewController は viewDidLoad で設定されます。

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;

この Container Controller は、YES を返すように自動的に ForwardAppearanceAndRotationMethodsToChildViewControllers を実装しています。また、非アクティブな ViewController への前方回転の変更を手動で実装しています。

- (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];
    }
}

メニュー ボタンがタップされると、ViewController 間の遷移を行います。

- (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;
    }];
}

このトランジションは、「通常の」ViewController では正常に機能し、アクティブでない場合でも、方向の変更後に適切に表示されます。ただし、SecondCV と呼ばれるこれらの子ビュー コントローラーの 1 つには、子ビュー コントローラーとしてUIPageViewControllerがあります。UIPageViewControllerDelegateとUIPageViewControllerDataSourceをこの SecondCV と pageViewController:spineLocationForInterfaceOrientation に設定していますポートレートの場合は UIPageViewControllerSpineLocationMin を返し、ランドスケープの場合は UIPageViewControllerSpineLocationMid を返します。この SecondVC の回転は、アクティブなときに正しく機能します。横向きモードで 2 ページ、縦向きモードで 1 ページが正しく表示されます。しかし、この SecondVC がアクティブでない場合、ローテーションは正しくありません。pageViewController:spineLocationForInterfaceOrientation:が呼び出された場合でも、ポートレート モードとランドスケープ モードの両方で 1 つのページがまだ存在します。しばらくこれを修正しようとしていますが、他のオプションが表示されません。これを修正する方法はありますか?

ありがとうございました。

4

2 に答える 2

1

このようなトリッキーを解決しました。

実際、SecondVC は正しく回転しているか、少なくとも背骨の位置が正しく変更されています。したがって、デバイスを回転させ、SecondVC が親ビューでアクティブなビュー コントローラーではなかった場合、回転メッセージを受け取り、 pageViewController :spineLocationForInterfaceOrientation:も呼び出されました。新しいインターフェイスの向きに合わせて背骨の正しい位置を設定し、setViewControllersを SecondVC のページ ビュー コントローラーに呼び出しました。問題は、UIPageViewControllerSpineLocationMid に 2 つのビュー コントローラーを設定しても、SecondVCを表示した後、UIPageViewControllerSpineLocationMin のように 1 つしかないことでした。

ビューコントローラーをviewWillAppearのページビューコントローラーに設定することで、問題を「修正」しました。このメソッドが呼び出されると、ページビューコントローラーのスパインの位置が正しくなり、この情報に基づいて1つまたは2つのページを設定するためです(ビューコントローラー)

- (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;
}

私は同じことを 2 回行っていることは知っていますが、この場合、私が支払っても構わないと思っている代償です。pageViewController:spineLocationForInterfaceOrientation:を呼び出してランドスケープ用に 2 つのビュー コントローラーを設定した後、SecondVC が非アクティブのときに、viewWillAppear:の m_pageController の ViewController が 1 つだけになるのはなぜですか。

于 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 に答える