如何让iPhone屏幕尺寸进行计算?
答案 0 :(得分:97)
您可以在UIScreen实例上使用bounds
属性:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
大多数人都想要主屏幕;如果你有一台设备连接到电视,你可能想要迭代UIScreens并获得每个设备的界限。
UIScreen class docs的更多信息。
答案 1 :(得分:20)
这是一个快速的'解: (更新为swift 3.x)
let screenWidth = UIScreen.main.fixedCoordinateSpace.bounds.width
let screenHeight = UIScreen.main.fixedCoordinateSpace.bounds.height
这是以像素为单位报告的,并且"始终以纵向向上的方式反映[s]设备的屏幕尺寸" (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
答案 2 :(得分: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;
}
答案 3 :(得分:1)
如果您想知道某个视图的大小,请使用bounds
的属性UIView
;如果您想了解设备的屏幕尺寸,请使用[[UIScreen mainScreen] bounds]
。
答案 4 :(得分:1)
与上述相似但略显冗长:
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
答案 5 :(得分: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);
}
答案 6 :(得分:1)
以下是获取屏幕尺寸的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中。
这也在文档中。
答案 7 :(得分:0)
[[UIScreen mainScreen] bounds]返回物理屏幕尺寸,而不考虑设备方向。
如果您需要根据当前方向获取屏幕尺寸,请查看How to get orientation-dependent height and width of the screen?
答案 8 :(得分:0)
使用UIScreen
的界限是一种很好的方法,但您应该使用CGGeometry
函数来访问CGRect
的数据而不是直接访问它。请参阅CGGeometry
docs。
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = CGRectGetWidth(screenBound);
CGFloat screenHeight = CGRectGetHeight(screenBound);
答案 9 :(得分: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;
}
答案 10 :(得分:0)
对于Xamarian同伴-这是在Xamarin.iOS + C#中获取屏幕尺寸的方法:
var screenBound = UIScreen.MainScreen.Bounds;
var screenSize = screenBound.Size;
var height = screenSize.Height;
var width = screenSize.Width;
答案 11 :(得分:0)
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))