[英]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)中:
On the IPHONE/IPAD Mini: 在IPHONE / IPAD Mini上:
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). 我遵循在某处提到的方法,并创建了一个名为
RotationControlledViewController
的UINavigationController
的子类(下面的代码)。
Then I made LandscapeViewController
and PortraitViewController
which subclass UIViewController
. 然后,我使
LandscapeViewController
和PortraitViewController
成为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.
对我来说,这意味着迷你“以肖像”方式“思考”它,但是以某种方式不能正确更新视图控制器。
The IPAD obviously looks correct. 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.