2012-04-13 74 views
6

我有一個包含其他ViewControllers的UIViewController。初始視圖控制器設置在viewDidLoad中:容器視圖控制器中UIPageViewController的旋轉

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; 

此容器控制器已經實施automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers返回YES。它還實施manualy正向旋轉改變非活躍ViewControllers

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

這種轉變工作正常,我的「正常」 ViewControllers和他們方位的變化後正常顯示,即使他們不積極。但是,其中一個子視圖控制器稱爲SecondCV,它具有UIPageViewController作爲子視圖控制器。我有UIPageViewControllerDelegateUIPageViewControllerDataSource設置爲此SecondCV和pageViewController:spineLocationForInterfaceOrientation:我回UIPageViewControllerSpineLocationMin的肖像和UIPageViewControllerSpineLocationMid景觀。此SecondVC的旋轉功能在其處於活動狀態時正常工作 - 在橫向上有兩頁,在縱向模式下正確顯示了一頁。但是當這個SecondVC不活躍時,旋轉是不正確的。即使調用了pageViewController:spineLocationForInterfaceOrientation:,縱向和橫向模式中仍然有一個頁面。我試圖解決這一段時間,但我沒有看到任何其他選項。你有什麼想法如何解決這個問題?

謝謝。

回答

1

我已經解決了這種棘手的問題。

其實SecondVC旋轉正確,或至少,脊柱位置正確更改。因此,當我旋轉設備並且SecondVC在父視圖中未處於活動視圖控制器時,其HAS收到旋轉消息並且pageViewController:spineLocationForInterfaceOrientation:也被調用。我爲新的界面方向設置了正確的脊柱位置,並將其設置爲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之後,只有一個ViewController在m_pageController中的viewWillAppear:爲我保留了一個謎。

+0

我也使用頁面視圖控制器,我已經添加了VC到一個數組,但不知道過渡各種VC的正確方式,我如何移動到下一個VC使用頁面捲曲轉換。你可以請指導我或發佈一些示例代碼。 – 2012-11-28 07:11:57

+0

[鏈接](https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html)我想你可以在這裏找到所有的想法,如果沒有,請聯繫我 – DaNY 2012-12-06 10:48:44

+0

我還沒有解決方案,請看看我的問題,如果你能指導我。 「http://stackoverflow.com/questions/13776894/uipageviewcontroller-change-from-two-page-mode-to-one-page-mode-without-changi#comment18942540_13776894」 – 2012-12-08 11:44:18

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

希望它可以幫助別人!

相關問題