2013-02-28 117 views
2

我有UINavigationController堆棧中的所有UIViewController都是縱向的,除了最後一個橫向。我當前的實現顯示最後一個視圖控制器在縱向上推動,但我可以旋轉到橫向,而不能旋轉回肖像。 如何在推動視圖時強制旋轉到橫向?iOS 6 - 在UINavigationController中強制旋轉UIViewController

UINavigationController子類:

@interface RotationViewController : UINavigationController 

@end 

@implementation RotationViewController 

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    if (self.topViewController.class == [LoadingViewController class]) 
    { 
     return UIInterfaceOrientationMaskLandscape; 
    } 

    return UIInterfaceOrientationMaskPortrait; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    if (self.topViewController.class == [LoadingViewController class]) 
    { 
     return UIInterfaceOrientationLandscapeLeft; 
    } 

    return UIInterfaceOrientationPortrait; 
} 

@end 

,我想景觀僅視圖控制器:

@implementation LoadingViewController 

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscape; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationLandscapeLeft; 
} 

@end 

其他視圖控制器沒有實現任何的這些方法。

回答

1

我有相同的條件。這樣你就可以實現下面的代碼在你的應用

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO]; 
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(270)); 
    landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0); 
    [[self navigationController].view setTransform:landscapeTransform]; 
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 480, 320); 
    self.navigationController.navigationBar.frame = CGRectMake(0.0, 20.0, 480, 44.0); 
} 

-(void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; 
    CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(0)); 
    landscapeTransform = CGAffineTransformTranslate (landscapeTransform, 0.0, 0.0); 
    [[self navigationController].view setTransform:landscapeTransform]; 
    self.navigationController.view.bounds = CGRectMake(0.0, 0.0, 320, 480); 
    self.navigationController.navigationBar.frame = CGRectMake(0.0, 20.0, 320.0, 44.0); 
} 

#define degreesToRadian(X) (M_PI * (X)/180.0) 

注: -

上面的代碼實現對iphone。所以如果你使用ipad的話,請更改邊界。

你不需要實現在您的視圖控制器的

- (BOOL)shouldAutorotate 

- (NSUInteger)supportedInterfaceOrientations 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 

功能....所以除去這些方法....

+0

它的工作原理(我只是從我的導航控制器supportedInterfaceOrientation返回0),但我一直在尋找如何觸發自動旋轉時的答案推動視圖。這個解決方案更多是一個招... – 2013-02-28 06:14:38

+0

適合重新定向屏幕!但由於某種原因,狀態欄仍處於最高位置。當我重新調整設備以匹配屏幕時,狀態欄會轉到正確的位置,但是當我回到原始縱向時,屏幕會變成肖像。 (這些都沒有受到'supportedInterfaceOrientation'存在/缺失的影響。) – JohnK 2013-06-21 00:40:12

0

檢查How to handle different orientations in iOS 6.一個項目看便知有問題正是你需要的例子。

基本上你需要在你的viewcontroller(你想旋轉的那個)中嵌入一個自定義的導航控制器。添加以下方法在此自定義導航控制器

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

,並添加到您的視圖控制器應轉動:

- (BOOL)shouldAutorotate 
{ 
    return YES; 
} 

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

確保人像,風景右和左景觀取向在項目中啓用。然後,如果你想阻止一個特定視圖的一些方向:

– application:supportedInterfaceOrientationsForWindow: 
相關問題