在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:
这是我第一次看到整个屏幕的大小。第二次我得到的屏幕减去导航栏。
当前回答
我以前用过这些方便的方法:
- (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;
}
其他回答
斯威夫特3.0
的宽度
UIScreen.main.bounds.size.width
身高
UIScreen.main.bounds.size.height
利用这些struct来了解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)
}
我已经把上面的一些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宽度= [[UIScreen mainScreen] bounds].size.width; CGFloat height = [[UIScreen mainScreen]bounds].size.height;
现代的答案是:
如果你的应用在iPad上支持分屏视图,这个问题就变得有点复杂了。你需要窗口的大小,而不是屏幕的大小,这可能包含2个应用程序。窗口的大小也可能在运行时发生变化。
使用应用程序主窗口的大小:
UIApplication.shared.delegate?.window??.bounds.size ?? .zero
注意:上述方法在启动时可能会在窗口成为关键窗口之前得到错误的值。如果你只需要宽度,非常推荐以下方法:
UIApplication.shared.statusBarFrame.width
使用uisscreen .main.bounds的旧解决方案将返回设备的边界。如果你的应用程序运行在分屏视图模式,它会得到错误的尺寸。
当应用程序包含2个或2个以上的窗口且窗口大小较小时,最热答案中的Self.view.window可能会显示错误的大小。