代码之家  ›  专栏  ›  技术社区  ›  nontomatic

按屏幕高度获取iphone型号xs max

  •  1
  • nontomatic  · 技术社区  · 7 年前

    我正在尝试为xs max定制设计。之前我只是通过检查main.bound.screen.height来识别设备,根据此站点,对于xs max: https://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions ,应该返回896.0,对吗?

    xcode 10.0xs max模拟器返回812.0作为屏幕高度(xr也一样,它应该有相同的屏幕点)。知道怎么解决这个问题吗?

    enum IPhone: String {
      case SE, normal, plus, X, XSMax
    
      init?(height: CGFloat) {
        switch height {
        case 568: self = .SE
        case 667: self = .normal
        case 736: self = .plus
        case 812: self = .X
        case 896: self = .XSMax
        default:
          return nil
        }
      }
    }
    
    struct Design {
      static var iPhone = IPhone(height: UIScreen.main.bounds.height)!
    }
    

    在视图控制器的viewdidLoad()中:

    print("screen height: \(UIScreen.main.bounds.height), iPhone: \(Design.iPhone.rawValue)")
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   nontomatic    7 年前

    好的,经过一些搜索,我在这里找到了我的问题的答案: UIScreen MainScreen Bounds returning wrong size

    事实证明,xcode将通过浏览您提供的启动图像来猜测屏幕的大小。你能相信吗?因此,如果您没有为新的XS Max提供合适大小的图像,它将回落到它找到的最大的启动图像。这就是为什么在一个没有发布图片的新项目中,它工作得很好。

    我已经为xs max和xr提供了启动图像,现在屏幕大小是正确的。