如何在iOS中获取屏幕宽度和高度?

545

如何在 iOS 中获取屏幕的尺寸?

目前,我使用以下代码:

lCurrentWidth = self.view.frame.size.width;
lCurrentHeight = self.view.frame.size.height;

viewWillAppear:willAnimateRotationToInterfaceOrientation:duration: 方法中:

第一次获取整个屏幕大小。第二次获取的是屏幕减去导航栏的大小。


确保您的应用程序实际上是全屏的,而不是看到黑色条纹围绕它 - 如果是这样,那么这可能是您的解决方案 - bobobobo
自从苹果发布了带有刘海的设备以来,所有这些答案似乎都局限于这些设备上的安全区域。例如,在iPhone 14 Pro Max模拟器上,我从MainScreen和SharedApplication.Windows[0]中看到的高度为736(DIPs)。在3倍密度下,这相当于2208个像素。设备规格为2796个像素(相当于932个DIPs)。顶部和底部有大黑边。我还没有尝试调整故事板;我同意bobobobo的观点,目前的情况比这些旧答案更复杂。注意:从Xamarin.iOS调用。 - undefined
18个回答

5
现代的解决方法如下:
如果您的应用程序支持iPad上的分屏视图,这个问题会变得有点复杂。您需要的是窗口大小而不是屏幕的大小,因为屏幕可能包含2个应用程序。而窗口大小也可能在运行时发生变化。
使用应用程序的主窗口大小:
UIApplication.shared.delegate?.window??.bounds.size ?? .zero

注意:在启动时,在窗口成为关键窗口之前,上述方法可能会得到错误的值。如果你只需要宽度,下面的方法非常推荐:
UIApplication.shared.statusBarFrame.width

使用UIScreen.main.bounds的旧解决方案将返回设备的边界。如果您的应用程序在分割视图模式下运行,则会得到错误的尺寸。

self.view.window在最新的答案中可能会获得错误的大小,当应用程序包含2个或更多窗口并且窗口大小较小时。


4
如果您想获取屏幕宽度/高度,无论设备方向如何(适用于从横向方向启动的仅纵向视图控制器的大小设置):
CGFloat screenWidthInPoints = [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale;
CGFloat screenHeightInPoints = [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale;

[UIScreen mainScreen].nativeBounds <-- 来自文档 --> 以像素为单位测量的物理屏幕边界矩形。此矩形基于设备处于纵向朝上方向。该值不随设备旋转而改变。


3

以下是一种获取屏幕尺寸的快捷方式:

print(screenWidth)
print(screenHeight)

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
}

这些都是标准函数,在以下链接中包含:

https://github.com/goktugyil/EZSwiftExtensions


3

iOS 13+的答案(找到关键窗口代码的来源):

guard let window = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else {
    return
}

let windowWidth = window.frame.width
let windowHeight = window.frame.height

2

Swift 3.0

针对宽度

UIScreen.main.bounds.size.width

为了高度
UIScreen.main.bounds.size.height

1

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

CGFloat宽度=[[UIScreen mainScreen] bounds].size.width; CGFloat高度=[[UIScreen mainScreen]bounds ].size.height;

0
对于iPad分屏窗口,[[UIScreen mainScreen] bounds]无法正常工作,请使用[UIApplication sharedApplication].delegate.window.bounds代替。

-2

您可以将这些宏放置在pch文件中,并通过使用“SCREEN_WIDTH”在项目的任何位置使用它们

#define SCREEN_WIDTH                ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)

以及 "SCREEN_HEIGHT"

#define SCREEN_HEIGHT               ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation ==   UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)

使用示例:

CGSize calCulateSizze ;
calCulateSizze.width = SCREEN_WIDTH/2-8;
calCulateSizze.height = SCREEN_WIDTH/2-8;

你能否按照Markdown格式缩进你的代码?目前这段代码被社区用户在审核队列中标记为“低质量”。 - m4n0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接