【问题标题】:How to get device width and height?如何获取设备的宽度和高度?
【发布时间】:2014-07-27 21:46:47
【问题描述】:

在 Objective-C 中,我们可以使用以下代码获取设备的宽度和高度:

CGRect sizeRect = [UIScreen mainScreen].applicationFrame
float width = sizeRect.size.width
float height = sizeRect.size.height

如何在 Swift 中做到这一点?

【问题讨论】:

  • 仅供参考 - applicationFrame 没有给出屏幕尺寸。它给出了应用程序的大小。如果您想要屏幕大小,最好使用bounds 而不是applicationFrame
  • 是的,rmaddy 你是对的。我已经在我的应用程序中进行了这些更改。感谢您的回复。

标签: ios swift cocoa-touch uiscreen cgsize


【解决方案1】:

我没试过,但应该是..

var bounds = UIScreen.main.bounds
var width = bounds.size.width
var height = bounds.size.height

【讨论】:

  • 它给出了超出范围的值。当我使用你的代码时,它给了我类型错误。
  • 哦,,,是的,它的 CGFloat 不浮动 .. 现在检查
  • 为什么不使用 Swift 自动类型推断?就像: var bounds = UIScreen.mainScreen().bounds
  • @Aks 显式定义类型更安全,更易读。
  • 如何将 CGFloat 值显示为 Int?
【解决方案2】:

斯威夫特 4.2

let screenBounds = UIScreen.main.bounds
let width = screenBounds.width
let height = screenBounds.height

【讨论】:

  • 我为什么会得到:使用未解析的标识符“UIScreen”...?
  • 可能需要导入UIKit
【解决方案3】:

@Houssni 的回答是正确的,但由于我们在谈论 Swift,而且这种用例会经常出现,因此可以考虑扩展 CGRect,类似于:

extension CGRect {
    var wh: (w: CGFloat, h: CGFloat) {
        return (size.width, size.height)
    }
}

然后你可以像这样使用它:

let (width, height) = UIScreen.mainScreen().applicationFrame.wh

万岁! :)

【讨论】:

    【解决方案4】:

    如果你想在你的代码中使用它。给你。

    func iPhoneScreenSizes() {
        let bounds = UIScreen.main.bounds
        let height = bounds.size.height
    
        switch height {
        case 480.0:
            print("iPhone 3,4")
        case 568.0:
            print("iPhone 5")
        case 667.0:
            print("iPhone 6")
        case 736.0:
            print("iPhone 6+")
        case 812.0:
            print("iPhone X")
            print("iPhone XS")
            break
        case 896.0:
            print("iPhone XR")
            print("iPhone XS Max")
            break
        default:
            print("not an iPhone")
    
        }
    }
    

    【讨论】:

    • @Jeff“不是 iPhone”XD
    【解决方案5】:

    (Swift 3) 请记住,大多数宽度和高度值将基于设备的当前方向。如果您想要一个不基于旋转的一致值并提供如同纵向向上旋转一样的结果,请尝试 fixedCoordinateSpace

    let screenSize = UIScreen.main.fixedCoordinateSpace.bounds
    

    【讨论】:

      【解决方案6】:
      var sizeRect = UIScreen.mainScreen().applicationFrame
      var width    = sizeRect.size.width
      var height   = sizeRect.size.height
      

      就是这样,也测试过。

      【讨论】:

      • 也没有; LOL XD
      • 是的,就像我一样,我现在真的很害怕!如果我开始在Swift 中写所有内容而不使用分号,然后在ObjectiveC 中再次写,我可能会遇到Ω(n) 编译错误LOL
      • @MatteoGobbi 我考虑使用分号的好习惯...使用它们。
      【解决方案7】:

      由于您正在寻找设备屏幕尺寸,最简单的方法是:

      let screenSize = UIScreen.mainScreen().bounds.size
      let width = screenSize.width
      let height = screenSize.height
      

      【讨论】:

        【解决方案8】:

        虽然@Adam Smaka 的回答很接近,但在 Swift 3 中是这样的:

        let screenBounds = UIScreen.main.bounds
        let width = screenBounds.width
        let height = screenBounds.height
        

        【讨论】:

          【解决方案9】:

          UIScreen 对象定义与基于硬件的显示相关的属性。 iOS 设备有一个主屏幕和零个或多个附加屏幕。每个屏幕对象定义相关显示的边界矩形和其他有趣的属性

          苹果文档网址:

          https://developer.apple.com/reference/uikit/uiwindow/1621597-screen

          使用 swift 3.0 获取用户设备的高度/宽度

          let screenHeight = UIScreen.main.bounds.height
          let screenWidth = UIScreen.main.bounds.width
          

          【讨论】:

            【解决方案10】:

            这适用于 Xcode 12

            func iPhoneScreenSizes() {
                    let height = UIScreen.main.bounds.size.height
                    switch height {
                    case 480.0:
                        print("iPhone 3,4")
                    case 568.0:
                        print("iPhone 5 | iPod touch(7th gen)")
                    case 667.0:
                        print("iPhone 6 | iPhone SE(2nd gen) | iPhone 8")
                    case 736.0:
                        print("iPhone 6+ | iPhone 8+")
                    case 812.0:
                        print("iPhone X | iPhone XS | iPhone 11 Pro")
                    case 896.0:
                        print("iPhone XR | iPhone XS Max | iPhone 11 | iPhone 11 Pro Max")
                    default:
                        print("not an iPhone")
                    }
                }
            

            【讨论】:

              【解决方案11】:
               static func getDeviceType() -> String
                  {
                      var strDeviceType = ""
                      if UIDevice().userInterfaceIdiom == .phone {
                          switch UIScreen.main.nativeBounds.height {
                          case 1136:
                              strDeviceType = "iPhone 5 or 5S or 5C"
                          case 1334:
                              strDeviceType = "iPhone 6/6S/7/8"
                          case 1920, 2208:
                              strDeviceType = "iPhone 6+/6S+/7+/8+"
                          case 2436:
                              strDeviceType = "iPhone X"
                          case 2688:
                              strDeviceType = "iPhone Xs Max"
                          case 1792:
                              strDeviceType = "iPhone Xr"
                          default:
                              strDeviceType = "unknown"
                          }
                      }
                      return strDeviceType
                  }
              

              【讨论】:

                【解决方案12】:

                以下是可用函数中尺寸的更新列表:

                func iScreenSizes() {
                            let height = UIScreen.main.bounds.size.height
                            print("Device height: \(height)")
                            switch height {
                            case 480.0:
                                print("iPhone 3 | iPhone 4 | iPhone 4S")
                            case 568.0:
                                print("iPhone 5 | iPhone 5S | iPhone 5C | iPhone SE")
                            case 667.0:
                                print("iPhone 6 | iPhone 7 | iPhone 8 | iPhone SE(2nd gen)")
                            case 736.0:
                                print("iPhone 6+ | iPhone 7+ | iPhone 8+")
                            case 780.0:
                                print("iPhone 12 Mini")
                            case 812.0:
                                print("iPhone X | iPhone XS | iPhone 11 Pro")
                            case 844.0:
                                print("iPhone 12 | iPhone 12 Pro")
                            case 896.0:
                                print("iPhone XR | iPhone XS Max | iPhone 11 | iPhone 11 Pro Max")
                            case 926.0:
                                print("iPhone 12 Pro Max")
                            case 1024.0:
                                print("iPad 1st gen | iPad 2 | iPad 3rd gen | iPad mini | iPad 4th gen | iPad Air | iPad mini 2 | iPad mini 3 | iPad Air 2 | iPad mini 4 | iPad 5th gen | iPad 6th gen | iPad  mini 5")
                            case 1112.0:
                                print("iPad Pro 2nd gen 10.5'' | iPad Air 3")
                            case 1194.0:
                                print("iPad Pro 3rd gen 11.0'' | iPad Pro 4th gen 11.0''")
                            case 1366.0:
                                print("iPad Pro 1st gen 12.9'' | iPad 2nd gen 12.9'' | iPad 3rd gen 12.9'' | iPad Pro 4th gen 12.9''")
                            default:
                                print("not listed in function")
                            }
                        }
                

                【讨论】:

                  【解决方案13】:

                  在 Swift 4 中我不得不使用 NSScreen.main?.deviceDescription

                  let deviceDescription = NSScreen.main?.deviceDescription          
                  let screenSize = deviceDescription![.size]
                  let screenHeight = (screenSize as! NSSize).height
                  

                  【讨论】:

                    猜你喜欢
                    • 2012-11-07
                    • 2012-04-27
                    • 2017-02-08
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-12-17
                    • 1970-01-01
                    • 2016-12-08
                    • 2011-07-12
                    相关资源
                    最近更新 更多