2010-09-03 15 views

回答

88

您可以使用UIScreen的一个实例bounds属性:

CGRect screenBound = [[UIScreen mainScreen] bounds]; 
CGSize screenSize = screenBound.size; 
CGFloat screenWidth = screenSize.width; 
CGFloat screenHeight = screenSize.height; 

大多数人都希望在主屏幕;如果你有一台设备连接到电视机,你可能想要遍历UIScreens并获得每个设备的界限。

更多信息在UIScreen class docs

+0

这是工作,感谢您的回应。 – haisergeant 2010-09-03 12:18:04

+1

如果屏幕是视网膜屏幕,此代码不起作用 - 需要考虑[比例值](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScreen_Class/Reference/ UIScreen.html#// apple_ref/OCC/instp/UIScreen /刻度)。 – 2014-04-27 21:01:42

+0

这取决于你的尺寸的定义。返回的值以点为单位,在处理时通常是所需的单位,例如视图层次结构。如果你需要像素尺寸,是的,比例因子起作用;但根据我的经验,这是一种罕见的需求。 – Tim 2014-04-28 11:51:49

1

如果您想知道某个视图的大小,或者想要知道设备的屏幕大小,请使用UIView的属性bounds[[UIScreen mainScreen] bounds]

+0

感谢您的回复 – haisergeant 2010-09-03 12:18:20

1

相似但略有更简洁:

CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width; 
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height; 
8

使用此代码在iOS8上修复:

float SW; 
float SH; 
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 
if (([[[UIDevice currentDevice] systemVersion] floatValue]<8) && UIInterfaceOrientationIsLandscape(orientation)) 
{ 
    SW = [[UIScreen mainScreen] bounds].size.height; 
    SH = [[UIScreen mainScreen] bounds].size.width; 
} 
else 
{ 
    SW = [[UIScreen mainScreen] bounds].size.width; 
    SH = [[UIScreen mainScreen] bounds].size.height; 
} 
+0

如果将'height'和'width'分配给错误的变量。 – 2015-03-12 01:37:45

+0

@MurraySagal这就是重点,它检查方向,并在iOS 7和更低版本中设置高度和宽度,当它处于横向模式时。 – cortices 2015-06-30 07:02:23

0

使用UIScreen的边界是一个很好的方法,但您应该使用CGGeometry函数来访问CGRect的数据,而不是直接访问它。请参阅CGGeometrydocs

CGRect screenBound = [[UIScreen mainScreen] bounds]; 
CGFloat screenWidth = CGRectGetWidth(screenBound); 
CGFloat screenHeight = CGRectGetHeight(screenBound); 
1
float screen_Height; 
float screen_Width; 

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) { 
    screen_Height = [[UIScreen mainScreen] bounds].size.height; 
    screen_Width = [[UIScreen mainScreen] bounds].size.width; 
} else { 
    screen_Height = ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width); 
    screen_Width = ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height); 
} 
+0

是的,iOS 8'固定'主屏幕大小,现在所有使用'黑客'的旧应用程序必须更新:( – Coolant 2014-09-22 05:43:50

0

假设你想利用当前的方向考虑,使用:

float screen_width; 
float screen_height; 
float vers = [[[UIDevice currentDevice] systemVersion] floatValue]; 
UIInterfaceOrientation orient = [UIApplication sharedApplication].statusBarOrientation; 
if (vers < 8 && UIInterfaceOrientationIsLandscape(orient)) 
{ 
    screen_height = [[UIScreen mainScreen] bounds].size.width; 
    screen_width = [[UIScreen mainScreen] bounds].size.height; 
} 
else 
{ 
    screen_width = [[UIScreen mainScreen] bounds].size.width; 
    screen_height = [[UIScreen mainScreen] bounds].size.height; 
} 
13

这里是一个 '迅速' 解决方案: (更新了迅速3.X)

let screenWidth = UIScreen.main.fixedCoordinateSpace.bounds.width 
let screenHeight = UIScreen.main.fixedCoordinateSpace.bounds.height 

这个报告的点数不是像素,“总是反映设备在屏幕上的屏幕尺寸entation“(Apple Docs)。无需打扰UIAnnoyinglyLongDeviceOrientation!

如果你想要的宽度和高度,以反映设备的方向:

let screenWidth = UIScreen.main.bounds.width 
let screenHeight = UIScreen.main.bounds.height 

这里的宽度和高度将取决于屏幕是纵向还是横向触发器值。

这里是如何得到像素没有点测量屏幕尺寸:

let screenWidthInPixels = UIScreen.main.nativeBounds.width 
let screenHeightInPixels = UIScreen.main.nativeBounds.height 

这也“是基于在纵向上方向的装置上。。作为设备旋转此值不会改变” (Apple Docs)

请注意,迅速2.x和更低的,你应该使用UIScreen.mainScreen()代替UIScreen.main

1

这里是一个斯威夫特的方式来获得屏幕尺寸:

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 
} 

这些被包括作为标准功能here

这也是在文档中。

相关问题