2014-01-07 45 views
0

我有一個MovieViewController作爲UINavigationController的子模塊。我使用MPMoviePlayerViewController來播放由MovieViewController觸發的視頻流,並且在該視頻視圖中,它可以將方向更改爲橫向或縱向。我需要的是,當我點擊完成按鈕時,MovieViewController再次轉到肖像模式,因爲它只支持肖像模式。僅在縱向模式下強制使用UINavigationController

下面是代碼

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

- (NSUInteger) supportedInterfaceOrientations { 
    return(UIInterfaceOrientationMaskPortrait); 
} 

- (BOOL) shouldAutorotate { 
    return FALSE; 
} 

但是當我點擊完成按鈕它的崩潰, 「preferredInterfaceOrientationForPresentation必須返回一個支持的接口方向!」

注:我以模態方式調用moviePlayer。

NSURL *movieURL = [NSURL URLWithString:@"URL"]; 
player =[[MyMoviePlayerViewController alloc] 
             initWithContentURL:movieURL]; 
[self presentViewController:player animated:YES completion:nil]; 

所以在調用它之後,會出現將會關閉視圖的按鈕。問題是當我在橫向模式下觀看電影並點擊完成按鈕時,它因爲我只有1個支持的界面(肖像)而崩潰。

+0

考慮這種解決方案,它創建導航控制器上的類別,它其設置爲允許/ dissalowed方向http://stackoverflow.com/questions/26205269/force-view-controller-to-portrait-only – zonabi

回答

0

感謝所有的答案,但這是解決我所有問題的代碼。

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationPortrait; 
} 
+0

在哪裏你把這個代碼? –

2

你需要做幾件事情。

首先,刪除這一行 - 它已被棄用。

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

其次,繼承你的UINavigationController並添加以下代碼:

- (NSUInteger)supportedInterfaceOrientations 
{ 
    NSUInteger orientation = UIInterfaceOrientationMaskPortrait; 
    if ([self.navigationController.visibleViewController isMemberOfClass:[MPMoviePlayerViewController class]]) { 
     orientation = UIInterfaceOrientationMaskAll; 
    } 
    return orientation; 
} 

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

在這段代碼中,我假設你在MovieViewController的頂推你MPMovieController(通過你的navigationController)。

相關問題