如何在 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:
方法中:
第一次获取整个屏幕大小。第二次获取的是屏幕减去导航栏的大小。
如何在iOS中获取屏幕的尺寸?
你所贴出的代码存在问题,因为你假设视图大小与屏幕大小相同,但实际情况并非总是如此。如果你需要屏幕大小,应该查看代表屏幕本身的对象,就像这样:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
分屏视图的更新:Dmitry在评论中问道:
我该如何获取分屏视图中屏幕的大小?
上面提供的代码报告了屏幕的大小,即使在分屏模式下也是如此。当您使用分屏模式时,应用程序的窗口会发生变化。如果上面的代码没有给出您期望的信息,那么像原帖作者一样,您正在查看错误的对象。但在这种情况下,您应该查看窗口而不是屏幕,就像这样:
CGRect windowRect = self.view.window.frame;
CGFloat windowWidth = windowRect.size.width;
CGFloat windowHeight = windowRect.size.height;
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
[UIScreen scale]
。 - RobotbugsCGRectGetWidth()
和CGRectGetHeight()
而不是直接访问矩形的size
字段。我认为这对于屏幕矩形来说可能永远不会成为问题,但是我想这也是需要注意的事情。 - Caleb注意,[UIScreen mainScreen] 包含状态栏,如果你想要获取应用程序的框架(不包括状态栏),你应该使用
+ (CGFloat) window_height {
return [UIScreen mainScreen].applicationFrame.size.height;
}
+ (CGFloat) window_width {
return [UIScreen mainScreen].applicationFrame.size.width;
}
return [[UIScreen mainScreen] bounds].size.width;
。 - user5724570我已经将一些上面的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)")
return UIScreen.mainScreen().bounds].size.width
进行替换。 - Suhaib我以前使用过这些便捷方法:
- (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;
}
我知道这是一篇旧文章,但有时候我会发现定义像这样的常量很有用,这样我就不必担心:
#define DEVICE_SIZE [[[[UIApplication sharedApplication] keyWindow] rootViewController].view convertRect:[[UIScreen mainScreen] bounds] fromView:nil].size
以上的常数应该能够在设备旋转时返回正确的尺寸。获取尺寸的过程就像这样简单:
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];
UIAlertView
会将自己插入为keyWindow的rootViewController视图。我有一个逻辑来避免这种特殊情况,因为如果有一个警告视图弹出,用户就无法与键盘进行交互,所以通常不需要在那个时候获取键盘的框架。 - memmons使用这些Structs来了解当前设备在Swift中的有用信息。
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
static let SCREEN_MAX_LENGTH = max(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_HEIGHT)
static let SCREEN_MIN_LENGTH = min(ScreenSize.SCREEN_WIDTH, ScreenSize.SCREEN_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_IPHONE_X = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.SCREEN_MAX_LENGTH == 812.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)
static let iOS11 = (iOSVersion.SYS_VERSION_FLOAT >= 11.0 && iOSVersion.SYS_VERSION_FLOAT < 12.0)
static let iOS12 = (iOSVersion.SYS_VERSION_FLOAT >= 12.0 && iOSVersion.SYS_VERSION_FLOAT < 13.0)
static let iOS13 = (iOSVersion.SYS_VERSION_FLOAT >= 13.0 && iOSVersion.SYS_VERSION_FLOAT < 14.0)
static let iOS14 = (iOSVersion.SYS_VERSION_FLOAT >= 14.0 && iOSVersion.SYS_VERSION_FLOAT < 15.0)
static let iOS15 = (iOSVersion.SYS_VERSION_FLOAT >= 15.0 && iOSVersion.SYS_VERSION_FLOAT < 16.0)
static let iOS16 = (iOSVersion.SYS_VERSION_FLOAT >= 16.0 && iOSVersion.SYS_VERSION_FLOAT < 17.0)
}
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);
我已经更新到 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
}