1. 程式人生 > >iOS5,iOS6,iOS7中UITabBarController,UINavigationController等介面中的旋轉問題總結

iOS5,iOS6,iOS7中UITabBarController,UINavigationController等介面中的旋轉問題總結

今天需要在程式中的一個介面中實現橫屏和豎屏切換,而其他介面保持豎屏,實現的過程中遇到了若干問題,總結了一下,在這裡分享給大家。

遇到的問題如下:

1.如何在其中一個UIViewController中實現橫豎屏切換,其他UIViewController仍然只支援豎屏。

2.無論如何設定引數,所有介面都不支援橫豎屏切換。

3.介面中的橫豎屏切換正常,但是啟動頁面始終是橫屏。

在解決以上問題之前,先說明一下設定裝置旋轉的方式。

1.iOS5、iOS6、iOS7都可以在程式的info.plist中增加Supported interface orientations欄位,在該欄位的值中增加所需要的方向。

2.iOS6和iOS7可以在AppDelegate中增加以下程式碼,設定程式支援的方向:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskAll;
}

以上兩個方式是針對整個應用的,下面介紹的方法針對單個介面。

3.iOS5以前,可以通過在UIViewController或其子類(UITabBarController、UINavigationController等)中重寫以下方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

4.在iOS6之後,可以通過在UIViewController或其子類(UITabBarController、UINavigationController等)中重寫以下方法:
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

所以通過將以上全域性和單個頁面的方法結合,我們就可以實現只在某個頁面中實現橫豎屏切換。那下面我們就來說說如何解決一開始提出的三個問題:

1.當程式中存在UITabBarController時,可以通過category或者繼承UITabBarController,覆蓋以下方法:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return YES;
}

當程式中存在UINavigationController時,可以通過category或者繼承UINavigationController,覆蓋以下方法:
-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return YES;
}

最後,就可以在需要橫豎屏切換的頁面中實現以下方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

在不需要橫豎屏切換的頁面中實現以下方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

如果覺得頁面過多的話,可以給所有頁面加一個基類,把不需要橫豎屏切換的程式碼加到裡面,再在需要橫豎屏切換的頁面中覆蓋基類的方法即可。

2.第二個問題解決方法讓我鬱悶的時間最長,但是解決的時間也是最快的,原因是我的手機平時都預設豎屏鎖定,解開就好了。。。

3.第三個問題在Info.plist中增加欄位和AppDelegate中增加程式碼兩種方式的區別。這兩種方式的實現效果是一樣的,都可以增加全域性支援的方向。需要注意的是,AppDelegate中增加的程式碼僅在iOS6之後的系統生效,如果要支援iOS5以下的系統,還是需要在Info.plist中增加欄位。如果兩種方式都實現時,所增加的方向以AppDelegate中增加的程式碼為準;而啟動螢幕的方向,以在Info.plist中欄位順序為準。

舉個例子,如果在Info.plist中增加欄位的順序為Landscape(right), Landscape(left), Portrait,則啟動的時候為橫屏;如果在Info.plist中增加欄位的順序為Portrait, Landscape(right), Landscape(left),則啟動的時候為豎屏。

如果大家覺得對自己有幫助的話,還希望能幫頂一下,謝謝:)