在iOS中如何获得屏幕的尺寸?

目前,我使用:

lCurrentWidth = self.view.frame.size.width;
lCurrentHeight = self.view.frame.size.height;

在viewWillAppear:和willAnimateRotationToInterfaceOrientation:duration:

这是我第一次看到整个屏幕的大小。第二次我得到的屏幕减去导航栏。


当前回答

你可以把这些宏放在你的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;

其他回答

斯威夫特3.0

的宽度

UIScreen.main.bounds.size.width

身高

UIScreen.main.bounds.size.height

你可以把这些宏放在你的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;

对于iPad分割窗口,[[UIScreen mainScreen] bounds]不起作用,使用[UIApplication sharedApplication].delegate.window.bounds代替。

注意,[UIScreen mainScreen]包含状态栏,如果你想检索你的应用程序的框架(不包括状态栏),你应该使用

+ (CGFloat) window_height   {
    return [UIScreen mainScreen].applicationFrame.size.height;
}

+ (CGFloat) window_width   {
    return [UIScreen mainScreen].applicationFrame.size.width;
}

CGFloat宽度= [[UIScreen mainScreen] bounds].size.width; CGFloat height = [[UIScreen mainScreen]bounds].size.height;