2011-12-21 71 views
2

我有一個基於選項卡的應用程序,其中一個選項卡在縱向和橫向模式下均可用,其他所有其他均僅在肖像中可用。強制方向更改

我正在檢查選中的按鈕,以允許在shouldAutorotateToInterfaceOrientation中旋轉:但是當我處於橫向模式時,當我選擇不同的選項卡時,我需要加載該視圖控制器,但也強制我的應用程序進入普通肖像佈局模式。

似乎沒有一個明確的和首選的做法,我嘗試設置狀態欄的方向,但狀態欄是唯一移動的視覺元素。

任何提示和例子會很好,謝謝。

回答

7

我能夠做到這一點,但我現在警告你,這是一個黑客攻擊。

首先,我創建的UITabBarController類別稱爲 UITabBarController+SelectiveRotation

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]; 
} 

這允許每當所選擇的選項卡允許的取向的TabBar自由旋轉。確保你在創建你的UITabBarController(可能在你的應用程序委託)中導入這個文件。

然後,我有我的AppDelegate使自己的標籤欄的代表,我實現了這個方法:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 
    // this ensures that the view that will be shown is presented in a supportred rotation 
    UIViewController *c = [[UIViewController alloc]init]; 
    [viewController presentModalViewController:c animated:NO]; 
    [viewController dismissModalViewControllerAnimated:NO]; 
    [c release]; 
    if ([UIViewController respondsToSelector:@selector(attemptRotationToDeviceOrientation)]) { 
     // this ensures that the view will be presented in the orientation of the device 
     // This method is only supported on iOS 5.0. iOS 4.3 users may get a little dizzy. 
     [UIViewController attemptRotationToDeviceOrientation]; 
    } 
} 

此代碼第二位強制標籤欄當標籤旋轉到一個可接受的取向變化。例如,如果您轉到可旋轉的選項卡並轉到橫向,然後轉到僅支持縱向的選項卡,則會強制將方向更改爲縱向。在iOS 4.3及更早版本中,返回到旋轉的選項卡將以您放置的方向呈現它。我找不到解決方法。

我做到了這一切,因爲這是客戶的要求,但我不認爲這實際上是一個非常有用的設計。我沒有發現太多的視覺調整讓人迷惑,但是通過這種方法強制設備旋轉很刺激,因爲它是瞬間的。你可能想嘗試一下,看看你對此感覺如何。

+0

這種方法還可以解決不必要的方向變化時的UIWebView完成播放全屏視頻,並迫使你的肖像的UIViewController景觀(由UIMoviePlayerController的'造成的 - (BOOL)setUIOrientation:(INT)方向動畫:(布爾)動畫強制:(布爾)強制;') – 2012-06-20 06:41:01

+0

我不熟悉該API,我無法在文檔中找到它。那是你寫的東西嗎? – 2012-06-21 19:02:46

+1

如果您的應用程序僅支持縱向模式,則UIMoviePlayerController由'UIWebView'分配,如果您的應用程序僅支持縱向模式,則UIMoviePlayerController可以強制將方向更改爲橫向,然後在單擊「完成「按鈕。附:私人API – 2012-06-22 03:33:47

0

我想如果你只是使用下面的代碼,它應該很好。

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 
// this ensures that the view that will be shown is presented in a supportred rotation 
UIViewController *c = [[UIViewController alloc]init]; 
[viewController presentModalViewController:c animated:NO]; 
[viewController dismissModalViewControllerAnimated:NO]; 
[c release]; 
if ([UIViewController respondsToSelector:@selector(attemptRotationToDeviceOrientation)]) { 
    // this ensures that the view will be presented in the orientation of the device 
    // This method is only supported on iOS 5.0. iOS 4.3 users may get a little dizzy. 
    [UIViewController attemptRotationToDeviceOrientation]; 
} 

}