iOS 6.0中的界面方向

25 iphone xcode ipad ios ios6

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

shouldAutorotate

supportedInterfaceOrientations

preferredInterfaceOrientationForPresentation
Run Code Online (Sandbox Code Playgroud)

由于"shouldAutorotateToInterfaceOrientation"在iOS 6.0中已弃用.

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

谢谢.

uer*_*ceg 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);
}
Run Code Online (Sandbox Code Playgroud)

iOS 6中的替换以及上述不推荐使用的iOS 5方法的等价物:

- (BOOL) shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


[编辑#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;
}
Run Code Online (Sandbox Code Playgroud)

[#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;
}
Run Code Online (Sandbox Code Playgroud)


Jam*_*tin 11

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


Nic*_*ari 7

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

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

首先,我在Xcode - > Target - > Summary中启用了Portrait,Landscape Left和Landscape.

在UINavigationController子类的实现中,我#import编辑 <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;
}
Run Code Online (Sandbox Code Playgroud)


小智 1

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

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

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

您还应该检查 UIKit 下的 iOS6 发行说明,遗憾的是我无法给您直接链接,因为我是新人。

由于 NDA,避免在此处发布代码

希望有帮助