2012-12-14 67 views
7

我有一個位於UINavigationController內的主視圖控制器。在該主視圖控制器中,我有一個按鈕,它將一個UIWebView的詳細視圖控制器放入其中。我希望這個細節視圖控制器在加載時處於橫向模式。回到主視圖控制器,它會強制返回到人像模式。我在這上面運行iOS 6。Objective-C中的iOS 6強制橫向方向

我已經看到了其他類似的問題,但它並沒有在我的工作。我創建了一個LandscapeViewController那的UIViewController的一個子類,我已經寫了這些方法:

#pragma mark - Orientation Methods 
- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

這是我的代碼時,我推詳細視圖控制器:

DetailViewController *detailVC = [[DetailViewController alloc] 
             initWithNibName:@"DetailViewController" 
             bundle:nil]; 

    [self.navigationController pushViewController:detailVC 
             animated:YES]; 

我想在哪裏在上面的代碼中對我的LandscapeViewController進行子類化,以使其工作,或者如何正確地繼承和推送我的詳細視圖控制器。如果導航控制器無法將我的詳細視圖控制器從縱向推向橫向,我也可以模式地呈現我的詳細視圖控制器。我在哪裏做錯了?

+0

這正是我腦子裏想的: http://stackoverflow.com/questions/11610819/iphone-forcefully-change-orientation-from-portrait-to-landscape-on-navigation?rq = 1 – jaytrixz

回答

8

考慮: 觀:只肖像 - 查看B:景觀僅

我不能這樣做,在導航控制器。相反,我所做的是打開一個從視圖A到視圖B的模態視圖,並強制一個新的導航控制器進入這個視圖。

這適用於iOS5 +。

您需要創建導航控制器這樣的類別:

的UINavigationController + Rotation_IOS6.h

#import <UIKit/UIKit.h> 

@interface UINavigationController (Rotation_IOS6) 

- (BOOL)shouldAutorotate; 
- (NSUInteger)supportedInterfaceOrientations; 
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation; 

@end 

的UINavigationController + Rotation_IOS6.h

#import "UINavigationController+Rotation_IOS6.h" 

@implementation UINavigationController (Rotation_IOS6) 

- (BOOL)shouldAutorotate 
{ 
    return [self.topViewController shouldAutorotate]; 
} 

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return [self.topViewController preferredInterfaceOrientationForPresentation]; 
} 

@end 

AppDelegate.m地址:

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

然後在

- (BOOL)shouldAutorotate { 
    return YES; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationPortrait; 
} 

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

視圖A打開視圖B做到這一點:

ViewB *vc = [[ViewB alloc] initWithNibName:@"ViewB" bundle:nil]; 

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc]; 

[self presentViewController:navigationController animated:YES completion:nil]; 

最後,在查看B

- (BOOL)shouldAutorotate { 
    return YES; 
} 

-(NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationLandscapeRight; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft); 
} 
0

他們有點擰在iOS 6關於這個問題。以下是我迄今爲止所瞭解的內容:

首先,Apple在Xcode 4.5中添加了「Supported Interface Orientations」按鈕。這對應於_info.plist中的「支持的接口方向」屬性。這些按鈕必須在任何其他工作之前切換到正確的選項。 (如果按鈕似乎拒絕切換它可能是因爲info.plist被CVS或其他進程鎖定。)

接下來,必須設置屬性.window.rootViewController,並且必須指向「底部」查看堆棧中的控制器。通常這將是導航控制器或標籤控制器。

如果希望禁用所有旋轉,可以使用按鈕完成,或者可以在「底部」視圖控制器中實現「shouldAutorotate」方法並使其返回NO。 (如果此方法省略,則默認爲YES。)

儘管使用shouldAutorotate禁用了自動旋轉功能,但如果顯示了MPMoviePlayerViewController,它將自動旋轉。只有切換支持的界面方向按鈕才能防止出現這種情況。

如果想要有條件地自動控制其他視圖控制器,它會變得更加混亂。基本上,您的「底部」視圖控制器必須實現supportedInterfaceOrientations方法,並根據當前的topViewController返回適當的位掩碼。這可以通過查詢topViewController的舊的「shouldAutorotateToInterfaceOrientation」方法的例程完成,但它有點難看。即使這個方案不需要修改旋轉視圖控制器的代碼,你也需要修改剛剛旋轉的「下方」的VC來實現「supportedInterfaceOrientation」,否則該視圖將在返回時旋轉。 (至少這是一個簡單的複製/粘貼。)但似乎沒有人提出更好,更一般的方案。