允许iPhone 6 Plus的横向,但不允许其他iPhone [英] Allow landscape for iPhone 6 Plus but not other iPhones

查看:151
本文介绍了允许iPhone 6 Plus的横向,但不允许其他iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的通用应用程序中,我当前在窗口的根视图控制器中覆盖 supportedInterfaceOrientations ,以定义允许的方向。到目前为止,决定是基于设备的用户界面习语:

In my universal app I currently override supportedInterfaceOrientations in the window's root view controller to define the orientations that are allowed. Up until now the decision was based on the device's user interface idiom:

- (NSUInteger) supportedInterfaceOrientations
{
  if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
  else
    return UIInterfaceOrientationMaskAll;
}

现在我想改变它,这样我也可以支持景观iPhone 6 Plus,但不适用于其他iPhone。我可以想象一两个解决方案,但这些都很脆弱,并且可能会在Apple开始制造新设备时破坏。

Now I would like to change this so that I can also support landscape for the iPhone 6 Plus, but NOT for other iPhones. I can image one or two solutions, but these are all rather brittle and will probably break when Apple starts to make new devices.

在一个理想的世界里我想改变上面的方法看起来像下面的代码片段,其中决定基于设备的用户界面大小类而不是用户界面惯用语:

In an ideal world I would like to change the above method to look like the following snippet, where the decision is based on the device's user interface size class instead of the user interface idiom:

- (NSUInteger) supportedInterfaceOrientations
{
  // Note the hypothetical UIDevice method "landscapeSizeClass"
  if ([[UIDevice currentDevice] landscapeSizeClass] == UIUserInterfaceSizeClassCompact)
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
  else
    return UIInterfaceOrientationMaskAll;
}

是否有像这样神奇的 landscapeSizeClass 方法?我在各种课程参考和指南中看了一下,但没有找到任何有用的东西。或者有人可以提出一个类似通用和面向未来的不同解决方案吗?

Is there something like this magical landscapeSizeClass method somewhere in UIKit? I have looked around a bit in various class references and guides, but didn't find anything useful. Or can someone suggest a different solution that is similarly generic and future-proof?

请注意,我的应用程序以编程方式创建其UI,因此纯粹基于故事板的解决方案已经完成。此外,我的应用程序仍然需要支持iOS 7,所以我不能只是改变一切使用大小类。但我可以做的是在使用简单的iOS 8 API之前进行运行时检查。

Note that my app creates its UI programmatically, so purely storyboard-based solutions are out. Also my app still needs to support iOS 7 so I can't just change everything to use size classes. What I can do, though, is to make runtime checks before I use simple iOS 8 APIs.

推荐答案

<缺乏正式的Apple API,这是我提出的解决方法:

Lacking an official Apple API, this is the workaround that I've come up with:

- (NSUInteger) supportedInterfaceOrientations
{
  if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
  {
    // iPhone 5S and below: 320x480
    // iPhone 6: 375x667
    // iPhone 6 Plus: 414x736
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    // The way how UIScreen reports its bounds has changed in iOS 8.
    // Using MIN() and MAX() makes this code work for all iOS versions.
    CGFloat smallerDimension = MIN(screenSize.width, screenSize.height);
    CGFloat largerDimension = MAX(screenSize.width, screenSize.height);
    if (smallerDimension >= 400 && largerDimension >= 700)
      return UIInterfaceOrientationMaskAll;
    else
      return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
  }
  else
  {
    // Don't need to examine screen dimensions on iPad
    return UIInterfaceOrientationMaskAll;
  }
}

该片段假定尺寸高于a的屏幕半任意选择的尺寸适合于旋转。 - 任意,因为400x700的阈值包括iPhone 6 Plus,但不包括iPhone 6。

The snippet simply assumes that a screen with dimensions above a semi-arbitrarily chosen size is suitable for rotation. Semi-arbitrarily, because a threshold of 400x700 includes the iPhone 6 Plus, but excludes the iPhone 6.

虽然此解决方案相当简单,我完全喜欢,因为缺乏复杂性。我真的不需要区分设备,所以任何聪明的解决方案,例如 Jef的答案中的解决方案都是过度的我的目的。

Although this solution is rather simple, I like it exactly because of its lack of sophistication. I don't really need to distinguish exactly between devices, so any clever solutions such as the one in Jef's answer are overkill for my purposes.

这篇关于允许iPhone 6 Plus的横向,但不允许其他iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆