简体   繁体   English

自动旋转仅在设备上的模拟器中有效(IOS 7.1)

[英]Autorotation only working in simulator not on device (IOS 7.1)

I've tried various approaches posted on here to setup correctly AutoRotation in IOS7 using storyboards. 我尝试了这里发布的各种方法,以使用情节提要板在IOS7中正确设置自动旋转。 What I have "should" work as it works perfectly well in the simulator but when I load the code onto a device (iPad or iPhone) it doesn't rotate. 我“应该”执行的工作因为它在模拟器中可以很好地工作,但是当我将代码加载到设备(iPad或iPhone)上时,它不会旋转。

[UPDATE: Code now DOES rotate on iPad but not Mini or iPhone???] [更新:现在可以在iPad上旋转代码,但在Mini或iPhone上不能旋转代码?]

In the simulator (and IPAD): 在模拟器(和IPAD)中:

  • Navigate to controller: Loads the correct orientation 导航到控制器:加载正确的方向
  • Rotate the Controller: Only allows specified orientations 旋转控制器:仅允许指定方向

On the IPHONE/IPAD Mini: 在IPHONE / IPAD Mini上:

  • Navigate to Controller: Does NOT change orientation 导航到控制器:不更改方向
  • Rotate the Controller: Only allows specified orientations 旋转控制器:仅允许指定方向

I have NO idea what the difference is. 我不知道有什么区别。 If anybody has any suggestions it would be SUPER helpful because its kind of driving me crazy here. 如果有人有任何建议,那将是超级有帮助的,因为这会使我发疯。

The approach I followed is detailed below: 我遵循的方法如下:

I followed the approach that was mentioned somewhere and I created a subclass of UINavigationController called RotationControlledViewController (code is below). 我遵循在某处提到的方法,并创建了一个名为RotationControlledViewControllerUINavigationController的子类(下面的代码)。

Then I made LandscapeViewController and PortraitViewController which subclass UIViewController . 然后,我使LandscapeViewControllerPortraitViewController成为UIViewController子类。 The View Controllers i want to be locked to a specific orientation inherit from these classes instead of UIViewController 我想锁定到特定方向的View Controller从这些类继承,而不是从UIViewController继承

(and yes - i did make sure my rotation lock was disabled on the device) (是的-我确实确保在设备上禁用了旋转锁)

RotationViewController 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

//
//  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

//
//  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

UPDATE -- Additional Anomalies: 更新-其他异常:

On IPAD Mini where things aren't exactly working correctly I am in landscape and navigate to a view that "should" be portrait. 在无法正常工作的IPAD Mini上,我处于横向并导航至“应”为人像的视图。 It launches an alert that is aligned portrait but the view itself comes up landscape. 它启动一个警报,该警报是纵向对齐的,但视图本身是横向的。 See the comparison: 查看比较:

I notice that the screen shots come out in portrait (as does the alert). 我注意到屏幕截图是纵向显示的(警报也是如此)。 Which implies to me the mini "thinks" its in portrait mode but somehow its not updating the view controller correctly. 对我来说,这意味着迷你“以肖像”方式“思考”它,但是以某种方式不能正确更新视图控制器。
IPAD Mini-警报得到正确的轮换?

The IPAD obviously looks correct. IPAD显然是正确的。 IPAD-一切正确

I made a sample project on GitHUB that demonstrates the issue: 我在GitHUB上做了一个示例项目来演示该问题:

https://github.com/jlss/RotationIssues https://github.com/jlss/RotationIssues

The situation you seem to be running into here is related to IOS7 onwards. 您似乎在这里遇到的情况与IOS7及更高版本有关。 From what I've seen the only way to do what you seek is to actually add a second navigation controller into your stack with a modal segue. 从我所看到的,做您要寻找的唯一方法是使用模式搜索将第二个导航控制器实际添加到堆栈中。 Alternatively you could try to turn off AutoLayout. 或者,您可以尝试关闭自动版式。 Neither of these options I think are what you want to hear. 我认为这些选项都不是您想听到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设置navigationBar的titleTextAttriubutes仅会使应用程序在ios 7.1设备上崩溃而不是模拟器 - Setting titleTextAttriubutes of navigationBar crashes the app only on ios 7.1 device not simulator 模拟器中的iOS 6自动旋转与实际的iOS 6设备不同 - iOS 6 autorotation in simulator varies from actual iOS 6 device UiNavigationController自动旋转在设备上不起作用? - UiNavigationController Autorotation not Working on Device? iOS 11设备方向自动旋转 - iOS 11 Device Orientation Autorotation 升级到iOS 9和MobileFirst 7.1后无法登录设备或模拟器上的应用程序 - Unable to login to app on device or simulator after upgrade to iOS 9 and MobileFirst 7.1 ios:定时器在模拟器中工作但不在设备上工作 - ios: Timer working in simulator but not on device Tabbarcontroller在模拟器上工作而不在iOS设备上 - Tabbarcontroller is working in simulator not on iOS device 黑屏中带有iOS 7.1的iOS模拟器,重置内容和设置不起作用 - iOS Simulator with iOS 7.1 in Black Screen, Reset Content and Settings not working iOS 8:没有故事板,自动旋转无法运行 - iOS 8: Autorotation is not working without storyboard UIImagePickerController在iOS设备上工作但在iOS模拟器上没有? - UIImagePickerController working on iOS device but not on iOS simulator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM