【发布时间】:2011-04-07 19:42:51
【问题描述】:
如何获取iPhone屏幕尺寸给计算?
【问题讨论】:
标签: ios iphone cocoa-touch uiscreen
如何获取iPhone屏幕尺寸给计算?
【问题讨论】:
标签: ios iphone cocoa-touch uiscreen
您可以在 UIScreen 实例上使用 bounds 属性:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
大多数人会想要主屏幕;如果您有一个连接到电视的设备,您可能希望遍历 UIScreens 并获取每个设备的边界。
更多信息请访问UIScreen class docs。
【讨论】:
这是一个“迅速”的解决方案: (为 swift 3.x 更新)
let screenWidth = UIScreen.main.fixedCoordinateSpace.bounds.width
let screenHeight = UIScreen.main.fixedCoordinateSpace.bounds.height
这以点而不是像素为单位报告,并且“始终以纵向向上的方向反映设备的屏幕尺寸”(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)
请注意,对于 swift 2.x 及更低版本,您应该使用 UIScreen.mainScreen() 而不是 UIScreen.main
【讨论】:
使用此代码在 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;
}
【讨论】:
height 和 width 被分配给错误的变量。
如果您想知道某个视图的大小,请使用UIView 的属性bounds,如果您想知道设备的屏幕大小,请使用[[UIScreen mainScreen] bounds]。
【讨论】:
与上面类似,但略显冗长:
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
【讨论】:
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);
}
【讨论】:
这是获取屏幕尺寸的 Swift 方法:
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。
这也在文档中。
【讨论】:
[[UIScreen mainScreen] bounds] 返回物理屏幕尺寸,不考虑设备方向。
如果您需要根据当前方向获取屏幕尺寸,请查看How to get orientation-dependent height and width of the screen?
【讨论】:
使用UIScreen 的边界是一个很好的方法,但您应该使用CGGeometry 函数来访问CGRect 的数据,而不是直接访问它。见CGGeometrydocs。
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = CGRectGetWidth(screenBound);
CGFloat screenHeight = CGRectGetHeight(screenBound);
【讨论】:
假设您要考虑当前方向,请使用:
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;
}
【讨论】:
对于 Xamarin 同胞 - 这是您在 Xamarin.iOS + C# 中获取屏幕大小的方法:
var screenBound = UIScreen.MainScreen.Bounds;
var screenSize = screenBound.Size;
var height = screenSize.Height;
var width = screenSize.Width;
【讨论】:
var PT: CGFloat = 0;
override func viewDidLoad() {
super.viewDidLoad()
setPoints();
}
func setPoints() {
let screenBounds = UIScreen.main.bounds
let screenScale = UIScreen.main.scale
let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)
let nativeBounds = UIScreen.main.nativeBounds
let nativeScale = UIScreen.main.nativeScale
let pxScreenWidth:CGFloat = nativeBounds.width;
let ptScreenWidth:CGFloat = screenBounds.width;
PT = pxScreenWidth/ptScreenWidth/nativeScale;
}
使用方法:
let label: UILabel = UILabel(frame: CGRect(x: 15 * PT, y: 10 * PT, width: self.view.frame.width - 30 * PT, height: self.view.frame.height - 20 * PT))
【讨论】: