2013-02-28 88 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: 
相关问题