【发布时间】:2014-06-02 06:12:11
【问题描述】:
我遇到了 Storyboards AutoRotation 和 iPhone 的问题。我已经将一个非常简单的项目与 3 个视图控制器放在一起,我将其上传到 gitHub 以演示该问题
我已在以下设备上对此进行了测试:
- IPAD 3
- iPhone 5s
- 模拟器(iphone / ipad)
在 iPad 和模拟器 (Iphone/Ipad) 上似乎一切正常,但是当我在 5s 上运行它时,它无法正确旋转。
在 iPhone 上,它会旋转警报视图,但不会旋转它出现的视图控制器
在模拟器上这是结果(看起来正确)
示例项目的代码可以在:
https://github.com/jlss/RotationIssues
我很高兴并愿意尝试任何人的任何建议。
注意事项:(我正在使用电话 objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft); )但据我所知,这应该没问题。
RotationControlledViewController.m
//
// 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
PortraitViewController.m
//
// 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.
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait);
}
- (void)viewDidAppear:(BOOL)animated {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Portrait Alert" message:@"This is portrait view" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
}
- (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
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];
objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft);
NSLog(@"Issuing a rotation message (hopefully");
}
- (void)viewDidAppear:(BOOL)animated {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Landscape Alert" message:@"This is landscape view" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
}
- (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
最后我有一个简单的故事板:
【问题讨论】:
-
setOrientation:是私有 API,可能导致从商店中删除和未定义的行为。 -
@Leo 那么你有什么建议呢? - 这个应用程序也永远不会去商店。此外,这里的许多答案都指的是该 API
-
是的,很多答案都提到了它,但这并不意味着它是正确的——你因为使用它而遇到了问题。如果您不会上传到应用商店,这不是问题,但最好使用适当的 API。当你想强制一个特定的方向时,最好的方法是使用模态表示。
-
你能提供一个示例 sn-p 或指出有用的地方吗?
-
另外,这是一个非法的 API:它就在这里:developer.apple.com/library/mac/documentation/Cocoa/Reference/…
标签: ios iphone objective-c ios7 storyboard