2012-03-20 51 views
1

我做了一些研究,但似乎無法找到答案,讓我的navigationController的rootViewController在啓動時正確無誤。我原來的問題在這裏:launch orientation of iPad is incorrect for landscape orientation (upside down)Force UIViewController orientation

在我的info.plist中,我將它設置爲支持兩種風景方向。如果我將我的rootViewController更改爲:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
// return UIInterfaceOrientationIsLandscape(interfaceOrientation); // original that does not work 
    return YES; 
} 

然後我的應用程序以正確的方向啓動。但是,我不想支持肖像模式。我只想支持風景模式。我想我可能會迫使取向,用做這樣的事情防止它切換到肖像模式:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) { 
     UIDeviceOrientation orientation = UIInterfaceOrientationLandscapeLeft; 
     [UIApplication sharedApplication].statusBarOrientation = orientation; 
    } 
    else { 

    } 

但這並不阻止應用旋轉到肖像模式。是否有可能強制方向?爲了使啓動方向正確(僅風景模式),是否還有其他事情需要我去做?謝謝!

回答

1

我剛剛創建了一個示例項目,將支持的界面方向(iPad)設置爲左橫向和右橫向(在info.plist中)。

然後我用:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return UIInterfaceOrientationIsLandscape(interfaceOrientation); 
} 

,它工作正常。它被迫景觀,不管我如何旋轉它。

您是否有任何其他視圖控制器可見,可能會返回YES到所有方向?這可能會混淆它。

+0

因爲我在我的AppDelegate中使用UINavigationController,這是一個問題嗎? – Crystal 2012-03-20 17:50:13

+0

只要在UINavigationController中使用的所有其他視圖控制器具有相同的shouldAutorotate策略,就不會有問題。 – bandejapaisa 2012-03-21 09:15:34

+0

嗯,我仍然有問題。當我的應用第一次啓動時,我必須根據需求顯示一個模式表單和一個UIAlertView。基本上這樣,其中tvc是我的viewController我模式顯示:'tvc.modalPresentationStyle = UIModalPresentationFormSheet; tvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizo​​ntal; [self presentModalViewController:tvc animated:YES];'。此ViewController的shouldAutoRotate也設置爲返回UIInterfaceOrientationIsLandscape(interfaceOrientation)。這個視圖控制器是否會導致它啓動不正確? – Crystal 2012-03-21 21:49:46