ios - technology - mac api




如何使用iOS上的代码获取屏幕大小? (8)

如何获得iPhone屏幕尺寸来计算?



与上述相似但略显冗长:

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

使用此代码修复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;
}

假设您想考虑当前的方向,请使用:

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;
}

对于同伴Xamarians - 这是你可以在Xamarin.iOS + C#中获得屏幕尺寸的方式:

var screenBound = UIScreen.MainScreen.Bounds;
var screenSize = screenBound.Size;
var height = screenSize.Height;
var width = screenSize.Width;

这是一个'迅捷'的解决方案:(更新为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


您可以在UIScreen的实例上使用bounds属性:

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

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

有关UIScreen类文档的更多信息。


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);
}




uiscreen