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

如何获得iOS屏幕的尺寸?

目前,我使用:

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

viewWillAppear: and willAnimateRotationToInterfaceOrientation:duration:

我第一次得到整个屏幕的大小。 第二次我得到屏幕减去导航栏。

如何获得iOS屏幕的尺寸?

您发布的代码存在的问题是,您需要依据视图大小来匹配屏幕大小,正如您所看到的,情况并非总是如此。 如果您需要屏幕大小,您应该查看代表屏幕本身的对象,如下所示:

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

更新分割视图:在评论中,德米特里问道:

如何在分割视图中获得屏幕大小?

上面给出的代码报告了屏幕的大小,即使在分屏模式下也是如此。 当你使用分屏模式时,你的应用程序的窗口会改变。 如果上面的代码不能给你所期望的信息,那么就像OP一样,你正在查看错误的对象。 在这种情况下,你应该看看窗口而不是屏幕,就像这样:

 CGRect windowRect = self.view.window.frame; CGFloat windowWidth = windowRect.size.width; CGFloat windowHeight = windowRect.size.height; 

小心,[UIScreen mainScreen]也包含状态栏,如果你想检索你的应用程序的框架(不包括状态栏),你应该使用

 + (CGFloat) window_height { return [UIScreen mainScreen].applicationFrame.size.height; } + (CGFloat) window_width { return [UIScreen mainScreen].applicationFrame.size.width; } 

我之前使用过这些便捷方法:

 - (CGRect)getScreenFrameForCurrentOrientation { return [self getScreenFrameForOrientation:[UIApplication sharedApplication].statusBarOrientation]; } - (CGRect)getScreenFrameForOrientation:(UIInterfaceOrientation)orientation { CGRect fullScreenRect = [[UIScreen mainScreen] bounds]; // implicitly in Portrait orientation. if (UIInterfaceOrientationIsLandscape(orientation)) { CGRect temp = CGRectZero; temp.size.width = fullScreenRect.size.height; temp.size.height = fullScreenRect.size.width; fullScreenRect = temp; } if (![[UIApplication sharedApplication] statusBarHidden]) { CGFloat statusBarHeight = 20; // Needs a better solution, FYI statusBarFrame reports wrong in some cases.. fullScreenRect.size.height -= statusBarHeight; } return fullScreenRect; } 

我已经将上面的Objective-C的一些答案翻译成了Swift代码。 每个翻译都是对原始答案的引用。

主要答复

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

简单的function答案

 func windowHeight() -> CGFloat { return UIScreen.mainScreen().applicationFrame.size.height } func windowWidth() -> CGFloat { return UIScreen.mainScreen().applicationFrame.size.width } 

设备定位答案

 var screenHeight : CGFloat let statusBarOrientation = UIApplication.sharedApplication().statusBarOrientation // it is important to do this after presentModalViewController:animated: if (statusBarOrientation != UIInterfaceOrientation.Portrait && statusBarOrientation != UIInterfaceOrientation.PortraitUpsideDown){ screenHeight = UIScreen.mainScreen().applicationFrame.size.width } else { screenHeight = UIScreen.mainScreen().applicationFrame.size.height } 

logging答案

 let screenWidth = UIScreen.mainScreen().bounds.size.width let screenHeight = UIScreen.mainScreen().bounds.size.height println("width: \(screenWidth)") println("height: \(screenHeight)") 

我意识到这是一个旧的post,但有时我觉得它是有用的#define这样的常量,所以我不必担心:

 #define DEVICE_SIZE [[[[UIApplication sharedApplication] keyWindow] rootViewController].view convertRect:[[UIScreen mainScreen] bounds] fromView:nil].size 

上述常数应该返回正确的大小,不pipe设备的方向。 然后获得维度是如此简单:

 lCurrentWidth = DEVICE_SIZE.width; lCurrentHeight = DEVICE_SIZE.height; 

获取设备大小以及考虑方向非常非常容易:

 // grab the window frame and adjust it for orientation UIView *rootView = [[[UIApplication sharedApplication] keyWindow] rootViewController].view; CGRect originalFrame = [[UIScreen mainScreen] bounds]; CGRect adjustedFrame = [rootView convertRect:originalFrame fromView:nil]; 

我们也必须考虑设备的方向:

 CGFloat screenHeight; // it is important to do this after presentModalViewController:animated: if ([[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIDeviceOrientationPortraitUpsideDown){ screenHeight = [UIScreen mainScreen].applicationFrame.size.height; } else{ screenHeight = [UIScreen mainScreen].applicationFrame.size.width; } 
 NSLog(@"%.0f", [[UIScreen mainScreen] bounds].size.width); NSLog(@"%.0f", [[UIScreen mainScreen] bounds].size.height); 

在这里,我已经更新了迅速3

应用程序框架已从iOS 9中弃用

在swift中,他们已经删除了(),他们改变了很less的命名约定,你可以在这里参考链接

 func windowHeight() -> CGFloat { return UIScreen.main.bounds.size.height } func windowWidth() -> CGFloat { return UIScreen.main.bounds.size.width } 

如果您希望屏幕宽度/高度与设备方向无关(适合从纵向方向启动纵向尺寸的视图控制器):

 CGFloat screenWidthInPoints = [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale; CGFloat screenHeightInPoints = [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale; 

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

这是一个迅速的方式来获得屏幕尺寸:

 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 } 

这些作为标准function包括在:

https://github.com/goktugyil/EZSwiftExtensions

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

使用这些Structs来了解有关Swift 3.0中当前设备的有用信息

 struct ScreenSize { // Answer to OP's question static let SCREEN_WIDTH = UIScreen.main.bounds.size.width static let SCREEN_HEIGHT = UIScreen.main.bounds.size.height } struct DeviceType { //Use this to check what is the device kind you're working with static let IS_IPHONE_4_OR_LESS = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH < 568.0 static let IS_IPHONE_SE = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 568.0 static let IS_IPHONE_7 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 667.0 static let IS_IPHONE_7PLUS = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 736.0 static let IS_IPAD = UIDevice.current.userInterfaceIdiom == .pad && ScreenSize.SCREEN_MAX_LENGTH == 1024.0 } struct iOSVersion { //Get current device's iOS version static let SYS_VERSION_FLOAT = (UIDevice.current.systemVersion as NSString).floatValue static let iOS7 = (iOSVersion.SYS_VERSION_FLOAT >= 7.0 && iOSVersion.SYS_VERSION_FLOAT < 8.0) static let iOS8 = (iOSVersion.SYS_VERSION_FLOAT >= 8.0 && iOSVersion.SYS_VERSION_FLOAT < 9.0) static let iOS9 = (iOSVersion.SYS_VERSION_FLOAT >= 9.0 && iOSVersion.SYS_VERSION_FLOAT < 10.0) static let iOS10 = (iOSVersion.SYS_VERSION_FLOAT >= 10.0 && iOSVersion.SYS_VERSION_FLOAT < 11.0) } 

迅速3.0

为宽度

 UIScreen.main.bounds.size.width 

为高度

 UIScreen.main.bounds.size.height 

现代的答案是:

如果你的应用程序支持iPad上的分割视图,这个问题就变得有点复杂了。 你需要窗口的大小,而不是屏幕的,可能包含2个应用程序。 运行时,窗口的大小也可能会有所不同。

使用应用的主窗口大小:

 UIApplication.shared.delegate?.window??.bounds.size ?? .zero 

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

 UIApplication.shared.statusBarFrame.width 

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

当最热门的答案self.view.window可能会得到错误的大小,当应用程序包含2个或更多的窗口,窗口小尺寸。

您可以将这些macros放在您的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;