iOS 6.0中的界面方向

25

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

shouldAutorotate

supportedInterfaceOrientations

preferredInterfaceOrientationForPresentation

"shouldAutorotateToInterfaceOrientation"在iOS 6.0中已被弃用。

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

谢谢。


1
您可以在此处查看支持iOS 6和iOS 5旋转的解决方案:https://dev59.com/1mct5IYBdhLWcg3wCZIT#12397738 - Sverrisson
1
请查看我的解决方案:https://dev59.com/A2cs5IYBdhLWcg3wrl4Z#12662433 - Carina
谢谢你的提问!我一直在尝试解决这个问题,因为事情并没有按照应该的方式进行。 - Justin
顺便提一下,如果任何答案对您有帮助,您应该考虑将其标记为答案。最好的问候。 - uerceg
5个回答

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,在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;
}

1
用这个替换了它,但不会影响损坏的方向。没有改变。 - Chewie The Chorkie
同样的问题。iPad应用程序应该横向运行,但现在是纵向运行。 - Joris Weimar
请检查我的编辑#1。这是我编写的UIViewController源代码,仅在纵向模式下运行。您可以更改UIInterfaceOrientations以获得所需的行为。由于我希望我的UIViewController以纵向模式显示,因此我还从Interface Builder中选择了Portrait作为我的UIViewController的首选方向。 - uerceg
我不理解的是:你使用iOS 6,但编译到4.3。在iOS 6设备上,应用程序无法正确旋转,除非你插入这些iOS 6版本。我认为这不是应该的工作方式。 - Duck
如果您能提供您代码所在的链接,那么我可以帮助查看哪里出了问题。最好的问候。 - uerceg
如果这仍然无效,请确保在您的应用程序委托中设置了根视图控制器,而不只是将子视图添加到窗口中:window.rootViewController = myViewController; - nicktmro

11

顺便提一下,您的Xcode项目设置现在具有优先权。请确保在项目设置中正确设置“支持的界面方向”数组。这就是我的问题所在。删除不需要的方向后,我的应用程序可以像我使用Xcode 4.4.1编译时一样工作。


1
请不要忘记(就像我一样)您必须为iPhone和iPad部署都执行此操作... - Eric D'Souza
我在Xcode项目设置中有Portatit和LandScape(左和右),但我意识到还有一对方向可以运行:UIDeviceOrientationFaceDown和UIDeviceOrientationFaceUp,你知道为什么吗? - Albert Català

7
我的应用程序有一个自定义的UINavigationController子类实例,它呈现多个视图控制器,所有视图控制器都是竖屏显示,除了播放视频时,我希望还可以允许两种横向方向。
基于@uerceg的答案,这是我的代码。
首先,在Xcode->目标->摘要中启用了纵向、左横向和右横向。
在UINavigationController子类的实现中,我 #import 了 。
然后我实现了这些方法:
// 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 Miari
这正是我试图解决的问题,但似乎 Xcode -> Target -> Summary 中的设置会覆盖一切。即使在 shouldAutorotate 函数中返回 NO,它仍然会旋转。有什么想法吗? - noodl_es
谢谢。顺便说一下,在iOS 6中,[self modalViewController]已经被弃用了。我用[self presentedViewController]替换它,现在可以正常工作了。 - dchakarov
既不是modalViewController,也不是presentedViewController对我有用。我使用了visibleViewController。 - Dmitry Khryukin

2

NicolasMiari的代码适用于我。稍微不同的是,我有一个presented UINavigationControllers的UITabBarController,并且我正在使用StoryBoards。 UITabBarController的子类实现完全相同,并且在Story Boards中为Tab Bar Controller选择类时要有耐心。即使构建后,它也不会立即可用。


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接