iOS 6.0中的界面方向

时间:2012-09-13 10:48:44

标签: iphone ios xcode ipad ios6

如何使用以下方法在iOS 6.0中支持界面方向:

shouldAutorotate

supportedInterfaceOrientations

preferredInterfaceOrientationForPresentation

在iOS 6.0中不推荐使用“shouldAutorotateToInterfaceOrientation”。

请提供代码段以支持您的答案。

感谢。

5 个答案:

答案 0 :(得分:19)

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,它在iPhone 6.0模拟器上的XCode 4.5中以纵向模式成功启动]

#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;
}

[#edit 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;
}

答案 1 :(得分:11)

顺便说一句,您的Xcode项目设置上的设置现在优先。 确保在项目设置中正确设置“支持的接口方向”阵列。 这对我来说是个问题。删除了不受欢迎的应用程序,我的应用程序就像我使用Xcode 4.4.1编译时那样工作。

答案 2 :(得分:7)

我的应用程序有一个自定义UINavigationController子类的实例,它提供了几个视图控制器,全部都是纵向的,除了在播放视频时,在这种情况下我想另外允许两个横向方向。

根据@uerceg的回答,这是我的代码。

首先,我在Xcode中启用了Portrait,Landscape Left和Landscape - >目标 - >总结

在UINavigationController子类的实现中,我#import'ed <MediaPlayer/MediaPlayer.h>

然后我实现了这些方法:

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

答案 3 :(得分:2)

NicolasMiari的代码为我工作。有点不同的旋转,我有一个呈现UINavigationControllers的UITabBarController,我正在使用StoryBoards。 UITabBarController的子类的实现完全相同,并且对故事板中标签栏控制器的类选择保持耐心。即使在建成后也无法立即使用。

答案 4 :(得分:1)

https://devforums.apple.com/thread/165384?tstart=0

https://devforums.apple.com/thread/166544?tstart=0

上述主题中有许多关于支持iOS6界面方向更改的示例和建议,这两个主题与游戏中心视图相关,但应该足以让您入门。

您还应该查看UIKit下的iOS6发行说明,遗憾的是,由于我是新手,我无法为您提供直接链接。

由于NDA而避免在此发布代码

希望有所帮助