【问题标题】:iOS 6 – Force a rotation for a UIViewController inside a UINavigationControlleriOS 6 – 强制 UINavigationController 内的 UIViewController 旋转
【发布时间】:2013-02-14 04:16:37
【问题描述】:

我有一个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

其他视图控制器不实现这些方法中的任何一个。

【问题讨论】:

    标签: ios cocoa-touch ios6


    【解决方案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
    

    你的视图控制器中的函数......所以删除那些方法......

    【讨论】:

    • 它可以工作(我只需要从我的导航控制器supportedInterfaceOrientation返回0),但我正在寻找关于如何在推送视图时触发自动旋转的答案。这个解决方案更像是一个技巧......
    • 非常适合重新定位屏幕!但由于某种原因,状态栏仍位于顶部。当我重新定位设备以匹配屏幕时,状态栏会转到正确的位置,但是当我回到原来的纵向时,屏幕会变成纵向。 (这些都不受supportedInterfaceOrientation 存在/不存在的影响。)
    【解决方案2】:

    检查问题How to handle different orientations in iOS 6. 查看那里的答案以获取您所需要的项目示例。

    基本上,您需要在视图控制器(您要旋转的那个)中嵌入一个自定义导航控制器。在此自定义导航控制器中添加以下方法

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

    并添加到应该旋转的视图控制器:

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

    确保在您的项目中启用纵向、横向右侧和横向左侧方向。然后,如果您想阻止特定视图的某些方向:

    – application:supportedInterfaceOrientationsForWindow:
    

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多