如何使用以下方法来支持iOS 6.0中的界面方向:
shouldAutorotate
supportedInterfaceOrientations
preferredInterfaceOrientationForPresentation
"shouldAutorotateToInterfaceOrientation"在iOS 6.0中已被弃用。
请提供支持您答案的代码片段。
谢谢。
如何使用以下方法来支持iOS 6.0中的界面方向:
shouldAutorotate
supportedInterfaceOrientations
preferredInterfaceOrientationForPresentation
"shouldAutorotateToInterfaceOrientation"在iOS 6.0中已被弃用。
请提供支持您答案的代码片段。
谢谢。
iOS 5中的弃用方法:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
iOS 6 中的替换方法及其对应的已弃用的 iOS 5 方法:
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
希望这可以帮到你。
[编辑 #1:添加了我的UIViewController,在XCode 4.5上成功在iPhone 6.0模拟器中以纵向模式启动]
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskPortrait;
}
[#编辑2:从仅支持iOS 5和iOS 6的横向应用程序的示例代码]
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
window.rootViewController = myViewController;
- nicktmro顺便提一下,您的Xcode项目设置现在具有优先权。请确保在项目设置中正确设置“支持的界面方向”数组。这就是我的问题所在。删除不需要的方向后,我的应用程序可以像我使用Xcode 4.4.1编译时一样工作。
// Autorotation (iOS <= 5.x)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([self modalViewController] && [[self modalViewController] isKindOfClass:[MPMoviePlayerController class]]) {
// Playing Video: Anything but 'Portrait (Upside down)' is OK
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
else{
// NOT Playing Video: Only 'Portrait' is OK
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
// Autorotation (iOS >= 6.0)
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if ([self modalViewController] && [[self modalViewController] isKindOfClass:[MPMoviePlayerController class]]) {
// Playing Video, additionally allow both landscape orientations:
orientations |= UIInterfaceOrientationMaskLandscapeLeft;
orientations |= UIInterfaceOrientationMaskLandscapeRight;
}
return orientations;
}
if
块中的第一个条件是多余的(如果模态视图控制器为nil,则isKindOfClass
测试应返回0,即“NO”)。当前版本更冗长,但可能更易于理解? - Nicolas MiariNicolasMiari的代码适用于我。稍微不同的是,我有一个presented UINavigationControllers的UITabBarController,并且我正在使用StoryBoards。 UITabBarController的子类实现完全相同,并且在Story Boards中为Tab Bar Controller选择类时要有耐心。即使构建后,它也不会立即可用。
https://devforums.apple.com/thread/165384?tstart=0
https://devforums.apple.com/thread/166544?tstart=0
在上面的帖子中,有许多关于支持iOS6界面方向更改的示例和建议,其中两个帖子与游戏中心视图相关,但应该足以让您入门。
您还应该检查UIKit下的iOS6发布说明,不幸的是,由于我是新手,无法为您提供直接链接。
由于NDA的原因,请避免在此处发布代码。
希望这可以帮助到您。