在iOS中如何获得屏幕的尺寸?
目前,我使用:
lCurrentWidth = self.view.frame.size.width;
lCurrentHeight = self.view.frame.size.height;
在viewWillAppear:和willAnimateRotationToInterfaceOrientation:duration:
这是我第一次看到整个屏幕的大小。第二次我得到的屏幕减去导航栏。
在iOS中如何获得屏幕的尺寸?
目前,我使用:
lCurrentWidth = self.view.frame.size.width;
lCurrentHeight = self.view.frame.size.height;
在viewWillAppear:和willAnimateRotationToInterfaceOrientation:duration:
这是我第一次看到整个屏幕的大小。第二次我得到的屏幕减去导航栏。
当前回答
注意,[UIScreen mainScreen]包含状态栏,如果你想检索你的应用程序的框架(不包括状态栏),你应该使用
+ (CGFloat) window_height {
return [UIScreen mainScreen].applicationFrame.size.height;
}
+ (CGFloat) window_width {
return [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
下面是一个快速获取屏幕尺寸的方法:
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
这里我已经更新了swift 3
applicationFrame已从iOS 9弃用
在swift 3中,他们已经删除了(),他们已经改变了一些命名惯例,你可以参考这里的链接
func windowHeight() -> CGFloat {
return UIScreen.main.bounds.size.height
}
func windowWidth() -> CGFloat {
return UIScreen.main.bounds.size.width
}
iOS 13+的答案(找到键窗口的代码来源):
guard let window = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else {
return
}
let windowWidth = window.frame.width
let windowHeight = window.frame.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)")