2012-06-13 34 views
1

我的iOS應用程序支持除PortraitUpsideDown以外的所有方向。 但在應用程序中,我有一個首選項,我希望它只能以縱向顯示。所以無論何時顯示此視圖,如果需要,它都會旋轉到縱向模式。這意味着用戶也將以縱向模式旋轉設備,以設置首選項,然後關閉此視圖界面後,現在應具有縱向方向。 問題是,隱藏的界面停留在橫向方向後,因爲我在顯示此視圖後阻止自動旋轉。 因此,隱藏視圖後,我想手動將界面更新爲當前設備方向。我該怎麼做?在iOS中手動更新界面方向


self.view.hidden=NO; 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.3]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 
self.view.alpha=1.0; 
[UIView commitAnimations]; 

此代碼是從OptionsViewController上它的父一LongPressGesture後調用。

回答

0

您所要做的就是將以下內容添加到您用於首選項的視圖控制器中。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
+0

我已經試過了。但它似乎並不奏效,因爲這個觀點是主視圖的子視圖,它支持所有的方向。所以如果我的主視圖會自動旋轉,首選視圖也會旋轉。或者我誤解了一些東西? – BartoNaz

+1

所以這個視圖是在你的viewcontroller的self.view中的子視圖?你只想讓這個特定的子視圖旋轉,而其他視圖保持靜態?那是你要的嗎? –

+0

不,我有一個由MainViewController控制的MainView和一個OptionsView,它是MainView的子視圖,由OptionsViewController控制。我希望MainView自動旋轉,但OptionsView在縱向模式下保持靜態。 – BartoNaz

0

調用此替代方法適用於我:

-(void)forceOrientationUpdate { 
    UIViewController *c = [[UIViewController alloc]init]; 
    [self presentModalViewController:c animated:NO]; 
    [self dismissModalViewControllerAnimated:NO]; 
    [c release]; 
} 
2

我創建了一個UIViewController擴展到強制控制器的取向更新的基礎上,由馬雷克R.提出由於iOS的新版本的解決方案,他的解決方案不再有效。我在這裏發佈我的解決方案,強制進行方向更新(以便考慮到支持的控制器方向方法),而不會有側視覺效果。希望能幫助到你。

UIViewController *vc = [[UIViewController alloc]init]; 
[[vc view] setBackgroundColor:[UIColor clearColor]]; 
[vc setModalPresentationStyle:(UIModalPresentationOverFullScreen)]; 
[self presentViewController:vc animated:NO completion:^{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [vc dismissViewControllerAnimated:YES completion:completion]; 
    }); 
}];