2012-06-21 29 views
0

是的,我知道這個問題已被問了很多次,但我找不到任何幫助我進一步的東西。如何強制我的iPhone應用程序保持特定的方向

使用具有3個viewcontrollers導航控制器,我需要從先前的屏幕保持數據,所以我不使用塞格斯但這樣的:

// When button is pressed 
- (IBAction)changeView:(id)sender { 
NSLog(@"Skipped connection screen"); 
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondView"]; 

[self.navigationController pushViewController:vc animated:YES]; 

} 其中SecondView是視圖控制器的標識符應該出現。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ 

return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 

,並在我的項目,我的Info.plist添加了:因爲我只想旋轉是在合適的水平,我在每一個.m文件我有我的觀點的頂部添加此代碼段Initial interface orientation = Landscape (right home button和我的項目設置中,我只添加了對此方向的支持。

問題是,當在iPhone上運行時,如果我以任何方式打開手機,方向會從橫向改變。當我嘗試將它恢復時,它只是不會。我想確保這個應用程序是從來沒有能夠遠離風景右轉。

有什麼建議嗎?非常感謝你提前。

+0

請參閱我的下面的答案,讓我知道你是否有任何問題。 – Iducool

回答

2

我認爲,如果你將以下鍵添加到您的.plist文件,那麼它會被固定。

"Supported interface orientations" 這個鍵的值將是"Landscape (right home button)"或任何你想要的,所以你的應用程序將只支持指定的方向。

還將此代碼添加到每個視圖控制器。

- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation{ 
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 
+0

這適用於應用程序範圍,一勞永逸的固定方向。如果你有很多不同的視圖控制器,這一個班輪是不容易的。 –

+0

我以爲我的項目設置 - >總結 - >支持的設備方向已經照顧到,但顯然不是。謝謝 – chwi

1

您應該使用的說法在你的「回報」代碼:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ 
    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 
+0

非常感謝,這工作。你能解釋爲什麼它沒有與我的代碼工作? – chwi

+1

@Wilhelmsen:你的代碼不工作,因爲你正在使用self.interfaceorientation。據我所知,self.interfaceorientation將執行shouldAutorotate後獲得價值。實際上,在ShouldAutorotate中,您必須比較系統參數中收到的值,並將其與您希望允許的方向進行比較。 – Iducool

相關問題