【问题标题】:Interface orientation in iOS 6.0iOS 6.0 中的界面方向
【发布时间】:2012-09-06 10:34:03
【问题描述】:

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

shouldAutorotate

supportedInterfaceOrientations

preferredInterfaceOrientationForPresentation

由于“shouldAutorotateToInterfaceOrientation”在 iOS 6.0 中已弃用。

请提供代码 sn-ps 以支持您的答案。

谢谢。

【问题讨论】:

  • 您可以在此处查看支持 iOS 6 和 iOS 5 轮换的解决方案:stackoverflow.com/questions/12396545/…
  • 在这里查看我的解决方案:stackoverflow.com/questions/12662240/…
  • 感谢您提出这个问题!一段时间以来,我一直在努力解决这个问题,因为事情并没有按照应有的方式进行。
  • 顺便说一句,如果任何答案对您有帮助,您应该考虑将其标记为答案。最好的问候。

标签: iphone ios xcode ipad ios6


【解决方案1】:

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

【讨论】:

  • 用这个替换它但不影响损坏的方向。没有任何改变。
  • 这里也一样。 ipad 应用程序应该运行横向但现在运行纵向
  • 检查我的编辑 #1。这是我只在纵向模式下运行的 UIViewController 的源代码。您可以更改 UIInterfaceOrientations 以获得所需的行为。由于我希望 UIViewController 以纵向模式显示,因此我还从 Interface Builder 中选择了纵向作为 UIViewController 的首选方向。
  • 我不明白的是:你使用 iOS 6 但编译到 4.3。除非您插入这些 iOS 6 版本,否则应用程序无法在 iOS 6 设备上正确旋转。我认为这不是应该的工作方式。
  • 如果你能提供粘贴代码的链接,我会帮助你看看有什么问题。最好的问候。
【解决方案2】:

顺便说一句,您的 Xcode 项目设置现在优先。 确保在项目设置中正确设置“支持的界面方向”数组。 这对我来说是个问题。删除了不需要的那些,我的应用程序就像我用 Xcode 4.4.1 编译时一样工作

【讨论】:

  • 别忘了(像我一样)你必须为 iPhone 和 iPad 部署执行此操作...
  • 我在 Xcode 项目设置中有 Portatit 和 LandScape(左和右),但我意识到有两个方向在运行:UIDeviceOrientationFaceDown 和 UIDeviceOrientationFaceUp,你知道为什么吗?
【解决方案3】:

我的应用有一个自定义 UINavigationController 子类的实例,它显示了几个视图控制器,所有视图控制器都只显示纵向,播放视频时除外,在这种情况下,我想另外允许两个横向显示。

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

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

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

【讨论】:

  • 实际上,if 块中的第一个条件是多余的(如果模态视图控制器为 nil,isKindOfClass 测试应该返回 0,即 NO)。当前版本更冗长,但也许更容易理解?
  • 这正是我试图解决的问题,但似乎 Xcode -> Target -> Summary 中的设置会覆盖所有内容。即使我在 shouldAutorotate 函数中返回 NO,它仍然会旋转。有什么想法吗?
  • 谢谢。顺便说一句,[self modalViewController] 在 iOS 6 中已被弃用。我用 [self presentViewController] 替换它,它工作正常。
  • modalViewController 和 presentViewController 都不适合我。使用了 visibleViewController
【解决方案4】:

NicolasMiari 的代码对我有用。有点不同的旋转我有一个呈现 UINavigationControllers 的 UITabBarController,我正在使用 StoryBoards。 UITabBarController 的子类实现完全一样,请耐心等待 Story Boards 中 Tab Bar Controller 的 Class 选择。即使在构建之后也不会立即可用。

【讨论】:

    【解决方案5】:

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

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

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

    您还应该查看 UIKit 下的 iOS6 发行说明,不幸的是,由于我是新手,因此无法直接给您链接。

    由于保密协议,避免在此处发布代码

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多