gpt4 book ai didi

ios - Autorotation 仅适用于模拟器而不适用于设备(​​IOS 7.1)

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:23:50 24 4
gpt4 key购买 nike

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

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

在模拟器(和 IPAD)中:

  • 导航到 Controller :加载正确的方向
  • 旋转 Controller :只允许指定方向

在 IPHONE/IPAD Mini 上:

  • 导航到 Controller :不改变方向
  • 旋转 Controller :只允许指定方向

我不知道有什么区别。如果有人有任何建议,那将非常有帮助,因为它让我发疯。

我采用的方法详述如下:

我按照某处提到的方法创建了 UINavigationController 的子类,名为 RotationControlledViewController(代码如下)。

然后我制作了 LandscapeViewControllerPortraitViewController,它们是 UIViewController 的子类。我想锁定到特定方向的 View Controller 继承自这些类而不是 UIViewController

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

旋转 View Controller

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

景观 View Controller

//
// 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 上,我处于横向模式并导航到“应该”为纵向的 View 。它会启动纵向对齐的警报,但 View 本身会出现横向。查看比较:

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

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

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

https://github.com/jlss/RotationIssues

最佳答案

您遇到的情况似乎与 IOS7 以上版本有关。据我所见,完成您所寻求的唯一方法是使用模态转场将第二个导航 Controller 实际添加到您的堆栈中。或者,您可以尝试关闭 AutoLayout。我认为这两种选择都不是您想听到的。

关于ios - Autorotation 仅适用于模拟器而不适用于设备(​​IOS 7.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23140211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com