iOS5,iOS6,iOS7中UITabBarController,UINavigationController等界面中的旋轉問題總結 – iPhone手機開發技術文章 iPhone軟體開發教學課程

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

遇到的問題如下:

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),則啟動的時候為豎屏。

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

個人博客:https://blog.csdn.net/zhaoxy2850
本文地址:https://blog.csdn.net/zhaoxy_thu/article/details/18412661
轉載請註明出處,謝謝!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *