如何在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问道。
怎样才能在分割视图中获得屏幕的大小?
上面给出的代码报告了屏幕的大小,即使在分屏模式下也是如此。当你使用分屏模式时,你的应用程序的窗口会发生变化。如果上面的代码没有给你预期的信息,那么就像OP一样,你看错对象了。不过在这种情况下,你应该看的是窗口而不是屏幕,像这样。
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
我'已经把上面的一些Objective-C答案翻译成了Swift代码。 每一个翻译的过程中都会参考原始答案。
let screen = UIScreen.main.bounds
let screenWidth = screen.size.width
let screenHeight = screen.size.height
func windowHeight() -> CGFloat {
return UIScreen.mainScreen().applicationFrame.size.height
}
func windowWidth() -> CGFloat {
return UIScreen.mainScreen().applicationFrame.size.width
}
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
}
let screenWidth = UIScreen.mainScreen().bounds.size.width
let screenHeight = UIScreen.mainScreen().bounds.size.height
println("width: \(screenWidth)")
println("height: \(screenHeight)")
我以前也用过这些方便的方法。
- (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];
我们也要考虑设备的方向。
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;
}
在这里,我已经更新了swift 3。
从iOS 9开始,applicationFrame被取消。
在swift three中,他们去掉了(),并且改变了一些命名习惯,你可以参考这里 [链接][1] 。
func windowHeight() -> CGFloat {
return UIScreen.main.bounds.size.height
}
func windowWidth() -> CGFloat {
return UIScreen.main.bounds.size.width
}
[1]: http://ashishkakkad.com/2016/10/refined-api-naming-migrating-to-swift-3/
利用这些 "结构 "来了解Swift 3.0中当前设备的有用信息。
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)
}
如果您希望屏幕宽度/高度不受设备方向的影响(适用于从横向方向启动的纵向视图控制器的大小)。
CGFloat screenWidthInPoints = [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale;
CGFloat screenHeightInPoints = [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale;
>.[UIScreen mainScreen].nativeBounds <--来自文档-->。 [UIScreen mainScreen].nativeBounds <--来自文档--> 物理屏幕的边界矩形,以像素为单位。 这个矩形是基于设备的纵向向上的方向。 这个值不会随着设备的旋转而改变。
这里有一个Swift获得屏幕尺寸的方法。
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
}
这些都是作为标准功能包含在:
现代的答案。
如果你的应用程序支持iPad上的分割视图,这个问题变得有点复杂。 你需要的是窗口'的大小,而不是屏幕'的大小,屏幕可能包含两个应用程序。 窗口'的大小也可能在运行时发生变化。
使用应用程序'的主窗口'的大小。
UIApplication.shared.delegate?.window??.bounds.size ?? .zero
注意:以上方法在启动时,在窗口成为关键窗口之前,可能会得到错误的数值。 上面的方法可能会在启动时,在窗口成为关键窗口之前得到错误的值。 如果你只需要宽度,下面的方法是非常推荐的。
UIApplication.shared.statusBarFrame.width
旧的解决方案使用UIScreen.main.bounds
将返回设备'的边界。
如果你的应用在分割视图模式下运行,会得到错误的尺寸。
最热门答案中的self.view.window
可能会在应用程序包含2个或更多窗口且窗口尺寸较小时得到错误的尺寸。
您可以将这些宏放在您的pch文件中,并通过使用"SCREEN_WIDTH"在项目的任何地方使用。
#define SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
和"SCREEN_HEIGHT"。
#define SCREEN_HEIGHT ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)
使用实例:
CGSize calCulateSizze ;
calCulateSizze.width = SCREEN_WIDTH/2-8;
calCulateSizze.height = SCREEN_WIDTH/2-8;