【问题标题】:supportedInterfaceOrientations called on all iPhones except 6+在所有 iPhone 上调用 supportInterfaceOrientations,除了 6+
【发布时间】:2015-10-02 01:02:01
【问题描述】:

在我的 iPhone 应用程序中,我将其限制为仅在项目目标部署信息下的纵向

我只想要一个横向页面,我使用supportedInterfaceOrientations 方法来获取它。

标准实现:

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Landscape
    }

它可以在所有 iPhone 设备和 iOS 版本上完美运行,iPhone 6+ 除外。从不调用supportedInterfaceOrientations 方法。

我找不到任何可能仅影响 iPhone 6+ 的原因,任何提示将不胜感激。

【问题讨论】:

  • 我刚刚创建了一个空的单视图应用程序,并验证了 supportedInterfaceOrientations() 已为 6 Plus 和 6 Plus 调用。除非视图控制器是导航 vc、拆分 vc、标签栏 vc 等其他东西的子视图控制器,否则无法立即看到您的代码有何不同。在这种情况下,请确保父视图控制器也响应您想。与我头顶的 6 Plus 不同的是,它能够在横向中使用拆分视图。也许那里有什么?
  • 最可能的原因是 iPhone 6+ 的尺寸等级将是常规宽度,但在 landscaoe 中呈现时,对于所有其他 iphone 来说都是紧凑宽度。它是什么样的控制器:普通的视图控制器或容器。如果是模态呈现,相信你需要使用preferredInterfaceOrientationForPresentation
  • 根据文档,supportedInterfaceOrientations 只会在根视图控制器上调用,或者在呈现为占据整个屏幕的控制器上调用。因此,您在 iPhone 6+ 上实现此功能的控制器可能不符合标准。它是什么样的控制器?似乎您通常应该在 UINavigationController、UITabBarController 和 UISplitViewController 等主容器控制器中实现 supportedInterfaceOrientations

标签: ios objective-c swift uikit iphone-6-plus


【解决方案1】:

你可以试试这个对我有用的代码

// you have to import foundation
import Foundation

class YourViewController : UIViewController {
    var device = self.platform()

     override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)

     if device.lowercaseString.rangeOfString("iphone6plus") != nil {
     supportedInterfaceOrientations()
    }

    }

    // add this method to your view controller
    func platform() -> String {
            var sysinfo = utsname()
            uname(&sysinfo) // ignore return value
            return NSString(bytes: &sysinfo.machine, length: Int(_SYS_NAMELEN), encoding:
                NSASCIIStringEncoding)! as String
        }

请注意,这不会在模拟器上运行,但会在实际设备上完美运行。

【讨论】:

    【解决方案2】:

    查看this question,试试这个代码sn-p:

    - (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 UIInterfaceOrientationMask.Landscape;
        else
          return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
      }
      else
      {
        // Don't need to examine screen dimensions on iPad
        return UIInterfaceOrientationMask.Landscape;
      }
    }
    

    -Herzbube

    缺乏官方的 Apple API,这是我想出的解决方法:

    [代码]

    sn-p 只是假设尺寸大于半任意选择尺寸的屏幕适合旋转。半随意,因为 400x700 的阈值包括 iPhone 6 Plus,但不包括 iPhone 6。

    虽然这个解决方案相当简单,但我喜欢它正是因为它缺乏复杂性。我真的不需要精确区分设备,所以任何聪明的解决方案,比如Jef's answer 中的解决方案,对我来说都是多余的。

    我所做的只是将第一个和第三个返回值从 UIInterfaceOrientationMaskAll 更改为 UIInterfaceOrientationMask.Landscape

    【讨论】:

    • 很抱歉,我可能没有将问题解释得足够清楚。我知道如何为特定设备实现景观。问题是在 iPhone 6+ 设备上没有调用 supportedInterfaceOrientations。这是 100% 的问题,它只是不会在 iPhone 6+ 设备上调用。
    • @JLoewy,您测试的不是实际设备吗?
    猜你喜欢
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    相关资源
    最近更新 更多