ios 如何在iOS中获取屏幕宽度和高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5677716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the screen width and height in iOS?
提问by griotspeak
How can one get the dimensions of the screen in iOS?
如何在iOS中获得屏幕的尺寸?
Currently, I use:
目前,我使用:
lCurrentWidth = self.view.frame.size.width;
lCurrentHeight = self.view.frame.size.height;
in viewWillAppear:
and willAnimateRotationToInterfaceOrientation:duration:
在viewWillAppear:
和willAnimateRotationToInterfaceOrientation:duration:
The first time I get the entire screen size. The second time i get the screen minus the nav bar.
我第一次得到整个屏幕尺寸。我第二次看到屏幕减去导航栏。
回答by Caleb
How can one get the dimensions of the screen in iOS?
如何在iOS中获得屏幕的尺寸?
The problem with the code that you posted is that you're counting on the view size to match that of the screen, and as you've seen that's not always the case. If you need the screen size, you should look at the object that represents the screen itself, like this:
您发布的代码的问题在于您指望视图大小与屏幕大小相匹配,正如您所见,情况并非总是如此。如果您需要屏幕尺寸,您应该查看代表屏幕本身的对象,如下所示:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
Update for split view:In comments, Dmitry asked:
拆分视图的更新:在评论中,德米特里问:
How can I get the size of the screen in the split view?
如何在拆分视图中获取屏幕的大小?
The code given above reports the size of the screen, even in split screen mode. When you use split screen mode, your app's window changes. If the code above doesn't give you the information you expect, then like the OP, you're looking at the wrong object. In this case, though, you should look at the window instead of the screen, like this:
上面给出的代码报告屏幕的大小,即使在分屏模式下也是如此。当您使用分屏模式时,您的应用程序的窗口会发生变化。如果上面的代码没有为您提供您期望的信息,那么就像 OP 一样,您正在查看错误的对象。但是,在这种情况下,您应该查看窗口而不是屏幕,如下所示:
CGRect windowRect = self.view.window.frame;
CGFloat windowWidth = windowRect.size.width;
CGFloat windowHeight = windowRect.size.height;
Swift 4.2
斯威夫特 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
回答by Ege Akpinar
Careful, [UIScreen mainScreen] contains status bar as well, if you want to retrieve the frame for your application (excluding status bar) you should use
小心,[UIScreen mainScreen] 也包含状态栏,如果你想为你的应用程序检索框架(不包括状态栏),你应该使用
+ (CGFloat) window_height {
return [UIScreen mainScreen].applicationFrame.size.height;
}
+ (CGFloat) window_width {
return [UIScreen mainScreen].applicationFrame.size.width;
}
回答by Martin Woolstenhulme
I've translated some of the above Objective-C answers into Swift code. Each translation is proceeded with a reference to the original answer.
我已经将上面的一些 Objective-C 答案翻译成 Swift 代码。每个翻译都参考原始答案。
Main Answer
主要答案
let screen = UIScreen.main.bounds
let screenWidth = screen.size.width
let screenHeight = screen.size.height
Simple Function Answer
简单的函数答案
func windowHeight() -> CGFloat {
return UIScreen.mainScreen().applicationFrame.size.height
}
func windowWidth() -> CGFloat {
return UIScreen.mainScreen().applicationFrame.size.width
}
Device Orientation Answer
设备方向答案
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
}
Log Answer
记录答案
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
println("width: \(screenWidth)")
println("height: \(screenHeight)")
回答by Luke Mcneice
I have used these convenience methods before:
我以前用过这些方便的方法:
- (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;
}
回答by A Random User
I realize that this is an old post, but sometimes I find it useful to #define constants like these so I do not have to worry about it:
我意识到这是一篇旧帖子,但有时我发现 #define 像这样的常量很有用,所以我不必担心:
#define DEVICE_SIZE [[[[UIApplication sharedApplication] keyWindow] rootViewController].view convertRect:[[UIScreen mainScreen] bounds] fromView:nil].size
The above constant should return the correct size no matter the device orientation. Then getting the dimensions is as simple as:
无论设备方向如何,上述常量都应返回正确的大小。然后获取尺寸非常简单:
lCurrentWidth = DEVICE_SIZE.width;
lCurrentHeight = DEVICE_SIZE.height;
回答by memmons
It's very, very easy to get your device size as well astake into account the orientation:
获取设备尺寸并考虑方向非常非常容易:
// 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];
回答by Oleh Kudinov
We have to consider the orientation of device too:
我们还必须考虑设备的方向:
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;
}
回答by Gaurav Gilani
NSLog(@"%.0f", [[UIScreen mainScreen] bounds].size.width);
NSLog(@"%.0f", [[UIScreen mainScreen] bounds].size.height);
回答by karthikeyan
Here i have updated for swift 3
在这里,我已经更新了swift 3
applicationFramedeprecated from iOS 9
applicationFrame从 iOS 9 弃用
In swift three they have removed () and they have changed few naming convention, you can refer here Link
在 swift 3 他们已经删除了 () 并且他们改变了一些命名约定,你可以参考这里链接
func windowHeight() -> CGFloat {
return UIScreen.main.bounds.size.height
}
func windowWidth() -> CGFloat {
return UIScreen.main.bounds.size.width
}
回答by leavez
The modern answer:
现代答案:
if your app support split view on iPad, this problem becomes a little complicated. You need window's size, not the screen's, which may contain 2 apps. Window's size may also vary while running.
如果你的应用支持 iPad 上的分屏,这个问题就变得有点复杂了。您需要窗口的大小,而不是屏幕的大小,它可能包含 2 个应用程序。运行时窗口的大小也可能会有所不同。
Use app's main window's size:
使用应用程序主窗口的大小:
UIApplication.shared.delegate?.window??.bounds.size ?? .zero
Note: The method above may get wrong value before window becoming key window when starting up. if you only need width, method below is very recommended:
注意:上述方法可能会在启动时在窗口成为关键窗口之前得到错误的值。如果您只需要宽度,非常推荐以下方法:
UIApplication.shared.statusBarFrame.width
The old solution using UIScreen.main.bounds
will return the device's bounds. If your app running in split view mode, it get the wrong dimensions.
使用的旧解决方案UIScreen.main.bounds
将返回设备的边界。如果您的应用在拆分视图模式下运行,则会获得错误的尺寸。
self.view.window
in the hottest answer may get wrong size when app contains 2 or more windows and the window have small size.
self.view.window
当应用程序包含 2 个或更多窗口并且窗口尺寸较小时,最热门的答案中的尺寸可能会错误。