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

目前,我使用:

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

在viewWillAppear:和willAnimateRotationToInterfaceOrientation:duration:

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


当前回答

我意识到这是一篇旧文章,但有时我发现#定义这样的常量很有用,这样我就不必担心它:

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

无论设备朝向如何,上述常量都应返回正确的大小。然后获取尺寸就像这样简单:

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

其他回答

我已经把上面的一些Objective-C答案翻译成Swift代码。每一篇译文都引用了原文的答案。

主要解决途径

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

##简单函数回答

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

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

##设备朝向回答

let screenHeight: CGFloat
let statusBarOrientation = UIApplication.shared.statusBarOrientation
// it is important to do this after presentModalViewController:animated:
if (statusBarOrientation != .portrait
    && statusBarOrientation != .portraitUpsideDown) {
    screenHeight = UIScreen.main.bounds.size.width
} else {
    screenHeight = UIScreen.main.bounds.size.height
}

# #日志回答

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

我们还必须考虑设备的朝向:

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;
}

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

你所发布的代码的问题是,你指望视图的大小与屏幕的大小相匹配,正如你所看到的,情况并不总是这样。如果你需要屏幕大小,你应该查看代表屏幕本身的对象,如下所示:

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

拆分视图更新:在评论中,Dmitry问道:

如何在分屏视图中获取屏幕的大小?

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

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

斯威夫特4.2

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

// split screen            
let windowRect = self.view.window?.frame
let windowWidth = windowRect?.size.width
let windowHeight = windowRect?.size.height

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

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

let windowWidth = window.frame.width
let windowHeight = window.frame.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];