自动旋转仅适用于不在设备上的模拟器(IOS 7.1)

时间:2014-04-17 17:59:05

标签: ios iphone objective-c rotation

我已尝试在此处发布各种方法,以便使用故事板在IOS7中正确设置AutoRotation。我有“应该”工作,因为它在模拟器中运行得非常好但是当我将代码加载到设备(iPad或iPhone)上时它不会旋转。

[更新:代码现在可以在iPad上旋转,但不能在Mini或iPhone上旋转???]

在模拟器(和IPAD)中:

  • 导航到控制器:加载正确的方向
  • 旋转控制器:仅允许指定方向

在IPHONE / IPAD Mini上:

  • 导航至控制器:不更改方向
  • 旋转控制器:仅允许指定方向

我不知道有什么区别。如果有人有任何建议,那将是超级有用的,因为它让我疯狂。

我遵循的方法详述如下:

我遵循了某处提到的方法,并创建了一个名为UINavigationController RotationControlledViewController的子类(代码如下)。

然后我制作了LandscapeViewControllerPortraitViewController子类UIViewController。视图控制器我希望锁定到从这些类继承而不是UIViewController

的特定方向

(是的 - 我确实确保我的旋转锁已在设备上禁用)

RotationViewController

//
//  RotationControlledViewController.m
//  ASGAARD
//
//  Created by Jeff Stein on 2/16/14.
//  Copyright (c) 2014 Jeff Stein. All rights reserved.
//

#import "RotationControlledViewController.h"

@interface RotationControlledViewController ()

@end

@implementation RotationControlledViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)shouldAutorotate
{
    BOOL ret =  [[self.viewControllers lastObject] shouldAutorotate];
//    NSLog(@"--Auto Roatate Reported %d", ret);
    return ret;
}


-(NSUInteger)supportedInterfaceOrientations
{
    NSUInteger ret = [[self.viewControllers lastObject] supportedInterfaceOrientations];

//    NSLog(@"--supportedInterfaceOrientations: %d", ret);


    return ret;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    UIInterfaceOrientation ret =  [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];

//    NSLog(@"--preferredInterfaceOrientationForPresentation: %ld",ret);
    return ret;
}


@end

LandscapeViewController

//
//  LandscapeViewController.m
//  ASGAARD
//
//  Created by Jeff Stein on 2/16/14.
//  Copyright (c) 2014 Jeff Stein. All rights reserved.
//

#import "LandscapeViewController.h"
#import "objc/message.h"

@interface LandscapeViewController ()

@end

@implementation LandscapeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
    objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);

    NSLog(@"Issuing a rotation message (hopefully");
}

-(void)viewDidAppear:(BOOL)animated {
    objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeLeft;
}

@end

PortraitViewController

//
//  PortraitViewController.m
//  ASGAARD
//
//  Created by Jeff Stein on 2/16/14.
//  Copyright (c) 2014 Jeff Stein. All rights reserved.
//

#import "PortraitViewController.h"
#import "objc/message.h"

@interface PortraitViewController ()

@end

@implementation PortraitViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

//    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];

    objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait);

}

- (void)viewDidAppear:(BOOL)animated {

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}



@end

更新 - 其他异常:

在IPAD Mini上,事情并没有完全正常工作我在风景中并导航到“应该”是肖像的视图。它会启动一个纵向对齐的警报,但视图本身会出现景观。见比较:

我注意到屏幕截图是纵向的(警报也是如此)。这对我来说意味着迷你“认为”它在纵向模式,但不知何故它没有正确更新视图控制器 IPAD Mini - alert gets correct rotation??

IPAD显然看起来很正确。 IPAD - everything is correct

我在GitHUB上制作了一个示例项目来演示这个问题:

https://github.com/jlss/RotationIssues

1 个答案:

答案 0 :(得分:1)

您似乎遇到的情况与IOS7以后有关。从我所看到的,你所寻求的唯一方法就是用模态segue将第二个导航控制器添加到你的堆栈中。或者,您可以尝试关闭AutoLayout。我认为这些选项都不是你想要听到的。