【问题标题】:How to get screen size using code on iOS?如何在 iOS 上使用代码获取屏幕大小?
【发布时间】:2011-04-07 19:42:51
【问题描述】:

如何获取iPhone屏幕尺寸给计算?

【问题讨论】:

    标签: ios iphone cocoa-touch uiscreen


    【解决方案1】:

    您可以在 UIScreen 实例上使用 bounds 属性:

    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;  
    CGFloat screenWidth = screenSize.width;
    CGFloat screenHeight = screenSize.height;
    

    大多数人会想要主屏幕;如果您有一个连接到电视的设备,您可能希望遍历 UIScreens 并获取每个设备的边界。

    更多信息请访问UIScreen class docs

    【讨论】:

    • 如果屏幕是 Retina 显示器,此代码不起作用 - 需要考虑到 scale value
    • 这取决于您对尺寸的定义。返回值以点为单位,在处理例如视图层次结构时,这通常是所需的单位。如果您需要像素尺寸,是的,比例因子发挥作用;然而,根据我的经验,这是一种罕见的需求。
    • 请记住,设备方向已被考虑在内。如果您要确定自己是否使用特定设备,请不要只假定纵向。
    • 如果您的应用仅设计为横向或纵向模式,您需要比较宽度和高度以确定正确的尺寸。我们不能再可靠地获得方向了。例如,我有一个专为纵向模式设计的应用程序,但如果它在 iPad 上以横向模式打开,则宽度和高度会混淆。
    【解决方案2】:

    这是一个“迅速”的解决方案: (为 swift 3.x 更新)

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

    这以点而不是像素为单位报告,并且“始终以纵向向上的方向反映设备的屏幕尺寸”(Apple Docs)。无需为 UIAnnoyinglyLongDeviceOrientation 烦恼!

    如果您希望宽度和高度反映设备方向:

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

    这里的宽度和高度将根据屏幕是纵向还是横向来翻转值。

    以下是如何获取以像素而非点为单位的屏幕尺寸:

    let screenWidthInPixels = UIScreen.main.nativeBounds.width
    let screenHeightInPixels = UIScreen.main.nativeBounds.height
    

    这也是“基于纵向向上的设备。该值不会随着设备的旋转而改变。”(Apple Docs)

    请注意,对于 swift 2.x 及更低版本,您应该使用 UIScreen.mainScreen() 而不是 UIScreen.main

    【讨论】:

      【解决方案3】:

      使用此代码在 iOS8 中修复:

      float SW;
      float SH;
      UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
      if (( [[[UIDevice currentDevice] systemVersion] floatValue]<8)  && UIInterfaceOrientationIsLandscape(orientation))
      {
          SW = [[UIScreen mainScreen] bounds].size.height;
          SH = [[UIScreen mainScreen] bounds].size.width;
      }
      else
      {
          SW = [[UIScreen mainScreen] bounds].size.width;
          SH = [[UIScreen mainScreen] bounds].size.height;
      }
      

      【讨论】:

      • 如果 heightwidth 被分配给错误的变量。
      • @MurraySagal 这就是重点,它在 iOS 7 及更低版本中检查方向并设置不同的高度和宽度,当它处于横向时。
      【解决方案4】:

      如果您想知道某个视图的大小,请使用UIView 的属性bounds,如果您想知道设备的屏幕大小,请使用[[UIScreen mainScreen] bounds]

      【讨论】:

        【解决方案5】:

        与上面类似,但略显冗长:

        CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
        CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
        

        【讨论】:

          【解决方案6】:
          float screen_Height;
          float screen_Width;
          
          if ([[[UIDevice currentDevice] systemVersion] floatValue]  >= 8) {
              screen_Height = [[UIScreen mainScreen] bounds].size.height;
              screen_Width = [[UIScreen mainScreen] bounds].size.width;
          } else {
              screen_Height = ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width);
              screen_Width = ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height);
          }
          

          【讨论】:

          • 是的,iOS 8“修复”了主屏幕尺寸,现在所有使用“hack”的旧应用程序都必须更新:(
          【解决方案7】:

          这是获取屏幕尺寸的 Swift 方法:

          var screenWidth: CGFloat {
              if UIInterfaceOrientationIsPortrait(screenOrientation) {
                  return UIScreen.mainScreen().bounds.size.width
              } else {
                  return UIScreen.mainScreen().bounds.size.height
              }
          }
          var screenHeight: CGFloat {
              if UIInterfaceOrientationIsPortrait(screenOrientation) {
                  return UIScreen.mainScreen().bounds.size.height
              } else {
                  return UIScreen.mainScreen().bounds.size.width
              }
          }
          var screenOrientation: UIInterfaceOrientation {
              return UIApplication.sharedApplication().statusBarOrientation
          }
          

          这些作为标准函数包含在here

          这也在文档中。

          【讨论】:

            【解决方案8】:

            [[UIScreen mainScreen] bounds] 返回物理屏幕尺寸,不考虑设备方向。

            如果您需要根据当前方向获取屏幕尺寸,请查看How to get orientation-dependent height and width of the screen?

            【讨论】:

              【解决方案9】:

              使用UIScreen 的边界是一个很好的方法,但您应该使用CGGeometry 函数来访问CGRect 的数据,而不是直接访问它。见CGGeometrydocs

              CGRect screenBound = [[UIScreen mainScreen] bounds];
              CGFloat screenWidth = CGRectGetWidth(screenBound);
              CGFloat screenHeight = CGRectGetHeight(screenBound);
              

              【讨论】:

                【解决方案10】:

                假设您要考虑当前方向,请使用:

                float screen_width;
                float screen_height;
                float vers = [[[UIDevice currentDevice] systemVersion] floatValue];
                UIInterfaceOrientation orient = [UIApplication sharedApplication].statusBarOrientation;
                if (vers < 8 && UIInterfaceOrientationIsLandscape(orient))
                {
                    screen_height = [[UIScreen mainScreen] bounds].size.width;
                    screen_width = [[UIScreen mainScreen] bounds].size.height;
                }
                else
                {
                    screen_width = [[UIScreen mainScreen] bounds].size.width;
                    screen_height = [[UIScreen mainScreen] bounds].size.height;
                }
                

                【讨论】:

                  【解决方案11】:

                  对于 Xamarin 同胞 - 这是您在 Xamarin.iOS + C# 中获取屏幕大小的方法:

                  var screenBound = UIScreen.MainScreen.Bounds;
                  var screenSize = screenBound.Size;
                  var height = screenSize.Height;
                  var width = screenSize.Width;
                  

                  【讨论】:

                    【解决方案12】:
                    var PT: CGFloat = 0;
                    
                    override func viewDidLoad() {
                    
                        super.viewDidLoad()
                        setPoints();
                    
                    }
                    
                    func setPoints() {
                    
                        let screenBounds = UIScreen.main.bounds
                    
                        let screenScale = UIScreen.main.scale
                    
                        let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)
                    
                        
                        let nativeBounds = UIScreen.main.nativeBounds
                    
                        let nativeScale = UIScreen.main.nativeScale
                    
                        
                        let pxScreenWidth:CGFloat = nativeBounds.width;
                    
                        let ptScreenWidth:CGFloat = screenBounds.width;
                    
                        PT = pxScreenWidth/ptScreenWidth/nativeScale;
                    
                    }
                    

                    使用方法:

                    let label: UILabel = UILabel(frame: CGRect(x: 15 * PT, y: 10 * PT, width: self.view.frame.width - 30 * PT, height: self.view.frame.height - 20 * PT))
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2019-01-10
                      相关资源
                      最近更新 更多