How to get screen size using code on iOS?

78,578

Solution 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.

Solution 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

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

Solution 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.

Solution 5

Similar to above but slightly less verbose:

CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
Share:
78,578
haisergeant
Author by

haisergeant

Tech geek

Updated on July 09, 2022

Comments

  • haisergeant
    haisergeant almost 2 years

    How to get the iPhone screen size to give calculation?

  • Matthew Herbst
    Matthew Herbst about 10 years
    This code doesn't work if the screen is a retina display - need to take into account the scale value.
  • Tim
    Tim about 10 years
    That depends on your definition of size. The returned values are in points, which is often the desired unit when dealing with, for example, a view hierarchy. If you need pixel dimensions, yes, the scale factor comes into play; in my experience, however, this is a rarer need.
  • Coolant
    Coolant over 9 years
    Yeah, the iOS 8 'fixed' the mainscreen size, and now all the old apps that used the 'hack', must be updated :(
  • Murray Sagal
    Murray Sagal about 9 years
    In the if height and width are being assigned to the wrong variable.
  • Murray Sagal
    Murray Sagal about 9 years
    Remember that the device orientation is taken into account. If you're trying to determine if you're on a specific device don't just assume portrait orientation.
  • cortices
    cortices almost 9 years
    @MurraySagal that's the point, it's checking the orientation and setting the height and width differently on iOS 7 and below, when it's in landscape.
  • Bobby
    Bobby about 8 years
    If your app is designed for only landscape or portrait mode you will need to compare the width to the height to determine the correct size. We cannot get the orientation reliably anymore. For example, I have an app designed only for portrait mode but if it is opened in landscape mode on an iPad the width and height are mixed up.