我有一個類,HomeView,作爲我的iPad應用程序上的UINavigationController的rootViewController。 info.plist文件中只設置橫向方向,HomeView不執行shouldRotateToInterfaceOrientation:
,但在模擬器旋轉時視圖在兩個方向都旋轉。如何禁用UINavigationController上的縱向方向rootViewController
我如何只在我的UIViewController中使用橫向方向?
我有一個類,HomeView,作爲我的iPad應用程序上的UINavigationController的rootViewController。 info.plist文件中只設置橫向方向,HomeView不執行shouldRotateToInterfaceOrientation:
,但在模擬器旋轉時視圖在兩個方向都旋轉。如何禁用UINavigationController上的縱向方向rootViewController
我如何只在我的UIViewController中使用橫向方向?
您不應該刪除shouldAutorotateToInterfaceOrientation方法;這不會阻止界面旋轉,它只會執行您在該方法中可能沒有的任何代碼。而應該嘗試這樣的事情(假設你永遠別希望接口旋轉):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
您可能想要避免完全阻止旋轉 - 相反,請考慮使用' - (BOOL)shouldAutorotateToInterfaceOrientation:'爲'UIInterfaceOrientationLandscapeLeft'或'UIInterfaceOrientationLandscapeRight'返回YES。 (另外,' - (BOOL)shouldAutorotateToInterfaceOrientation:'從iOS 6開始已棄用 - 你也希望' - (NSUInteger)supportedInterfaceOrientations') –
如果不實現shouldAutorotateToInterfaceOrientation:運行時將調用該方法的超類,UIViewController。
相反,你應該要旋轉的方向適當地應對方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
BOOL shouldAutorotate = NO;
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
shouldAutorotate = YES;
}
return shouldAutorotate;
}
看看在UIInterfaceOrientation參考頁和UIViewController's shouldAutorotateToInterfaceOrientation:參考頁面瞭解更多信息。
較短的版本:'return(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight); ' – matsr
確實如此,但較短的版本可能無法解釋爲什麼它是答案。只教他們copypasta。 –
值得注意的是,有四個方向。肖像,人像上下,風景左右和風景右側。你在你的plist.info文件中設置了哪些內容? –