问题
How to get the iPhone screen size to give calculation?
回答1:
You can use the bounds
property on an instance of UIScreen:
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
Most people will want the main screen; if you have a device attached to a TV, you may instead want to iterate over UIScreens and get the bounds for each.
More info at the UIScreen class docs.
回答2:
Here is a 'swift' solution: (Updated for swift 3.x)
let screenWidth = UIScreen.main.fixedCoordinateSpace.bounds.width
let screenHeight = UIScreen.main.fixedCoordinateSpace.bounds.height
This reports in points not pixels and "always reflect[s] the screen dimensions of the device in a portrait-up orientation"(Apple Docs). No need to bother with UIAnnoyinglyLongDeviceOrientation!
If you want the width and height to reflect the device orientation:
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
Here width and height will flip flop values depending on whether the screen is in portrait or landscape.
And here is how to get screen size measured in pixels not points:
let screenWidthInPixels = UIScreen.main.nativeBounds.width
let screenHeightInPixels = UIScreen.main.nativeBounds.height
This also "is based on the device in a portrait-up orientation. This value does not change as the device rotates."(Apple Docs)
Please note that for swift 2.x and lower, you should use UIScreen.mainScreen()
instead of UIScreen.main
回答3:
Use This Code to fix in 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;
}
回答4:
Use the property bounds
of UIView
if you want to know the size of a certain view or [[UIScreen mainScreen] bounds]
if you want to know the screen size of the device.
回答5:
Similar to above but slightly less verbose:
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
回答6:
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);
}
回答7:
Here is a Swift way to get screen sizes:
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
}
These are included as a standard function here.
This is also in the documentation.
回答8:
[[UIScreen mainScreen] bounds] returns physical screen dimensions not taking device orientation into account.
If you need to get screen size according to the current orientation please look at How to get orientation-dependent height and width of the screen?
回答9:
Using the UIScreen
's bounds is a good way to do it but you should us the the CGGeometry
functions to access a CGRect
's data rather than accessing it directly. See the CGGeometry
docs.
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = CGRectGetWidth(screenBound);
CGFloat screenHeight = CGRectGetHeight(screenBound);
回答10:
Assuming you want to take the current orientation into account, use:
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;
}
回答11:
For fellow Xamarians - here is the way you can get the screen size in Xamarin.iOS + C#:
var screenBound = UIScreen.MainScreen.Bounds;
var screenSize = screenBound.Size;
var height = screenSize.Height;
var width = screenSize.Width;
来源:https://stackoverflow.com/questions/3635483/how-to-get-screen-size-using-code-on-ios