1

我很困惑在ios中執行自動轉換。
我有一個UIViewController,我告訴它自動旋轉裏面shouldAutoRotateToInterfaceOrientation。但是,它不起作用。 因此,我閱讀了一些關於如何查看導航堆棧和/或是否使用UITabBarController的信息,因爲這已知會導致各種混淆(引發手動)。ios5中的自動轉換 - 爲什麼會失敗的最常見原因?

實際上,我有一個UITabBar和一個UINavigationController。我想旋轉的UIView被深入三層或四層「層」。

爲什麼不自動旋轉只能通過shouldAutoRotateToInterfaceOrientation在當前UIViewController內部返回來確定?

從位於蘋果的技術文檔:https://developer.apple.com/library/ios/#qa/qa2010/qa1688.html爲什麼一個UIViewController可能不會旋轉,當你期待它:

所有在你的UITabBarController或UINavigationController的子視圖控制器不上一個共同的方向一致組。

要確保你的所有子視圖控制器正常旋轉, 必須實現代表每個選項卡或導航級各 視圖控制器shouldAutorotateToInterfaceOrientation。每個必須 在旋轉發生的方向相同。也就是說,他們 都應該爲相同的方向位置返回YES。

有人可以總結一下我們在使用UITabBars和UINavigationControllers時需要注意的事實嗎?人們常常在哪裏搞砸或什麼是渾濁點?

如果您有UITabBar,UITabBarController或UINavigationController,則其所有子視圖都需要具有相同的自轉行爲。但肯定你可以有一些兒童的觀點旋轉,其他人不旋轉,對嗎?

我懷疑失敗的常見原因與理解響應者鏈不夠好,因爲它適用於自動轉換。如果是這樣,有人可以幫我解決這個問題嗎?那麼我可能會弄清楚「選擇性自動旋轉」。

要添加到此,其他帖子指出您必須繼承UITabBarController並覆蓋shouldAutorotateToInterfaceOrientation。通過UIViewControllers,當連接到的UITabBarController UINavigationController的,並提出了

回答

2

我想你希望他們都以同樣的方式迴應的原因是因爲它讓用戶感到尷尬。如果您在可旋轉的特殊視圖上處於橫向模式,則將該視圖控制器彈出到導航控制器上的父級或點擊不具有橫向模式的選項卡時,這將會是一種奇怪的體驗。

我認爲那是推理。

但是,如果需要,您可以捕獲設備方向通知並自己處理它們,如果設備在您要旋轉的特定視圖上旋轉,則推入新視圖。

使用此:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rotationDetected) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

然後創建一個函數rotationDetected來處理,當我們轉動會發生什麼......是這樣的:

-(void) rotationDetected { 

    if (([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)){ 

    //Push on the view in landscape orientation if we aren't there already 

} else if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) { 

    //If the landscape view is present, pop it back to the portait view 

} 
1

我2美分...

意見,不是靜態的,而是DINAMIC。我的意思是你必須認爲用戶可能會在你的觀點之間切換。如果它們中的一些旋轉而另一些不旋轉,那麼你的圖形用戶界面應該是混亂的,至少在蘋果看來是這樣。

0

我也碰到過同樣的問題,希望這個解決方案能夠幫助他人,

對於iOS 5和下面

實施類別來處理情況UINavigationControllers標籤相關情況:

@implementation UITabBarController (AutoRotation) 

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

{ 
      if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) 

      { 
       UIViewController *rootController = [((UINavigationController *)self.selectedViewController).viewControllers lastObject]; 
      return [rootController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; 

      } 
     return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation]; 

    } 

@end 

現在,一旦這一類實現自動旋轉完全由任何當前的UIViewController內shouldAutoRotateToInterfaceOrientation收益確定