How to get screen size using code on iOS?
Asked Answered
H

12

50

How to get the iPhone screen size to give calculation?

Haviland answered 3/9, 2010 at 12:2 Comment(0)
L
106

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.

Lintel answered 3/9, 2010 at 12:7 Comment(4)
This code doesn't work if the screen is a retina display - need to take into account the scale value.Gastroenteritis
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.Lintel
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.Gildus
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.Jacobs
S
26

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

Sarraute answered 1/3, 2015 at 6:7 Comment(0)
H
8

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;
}
Hers answered 24/3, 2014 at 3:56 Comment(2)
In the if height and width are being assigned to the wrong variable.Gildus
@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.Planimetry
S
1

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.

Smoothspoken answered 3/9, 2010 at 12:7 Comment(0)
P
1

Similar to above but slightly less verbose:

CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHeight = [[UIScreen mainScreen] bounds].size.height;
Peridotite answered 24/3, 2014 at 3:53 Comment(0)
J
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);
}
Jacobs answered 17/9, 2014 at 21:36 Comment(1)
Yeah, the iOS 8 'fixed' the mainscreen size, and now all the old apps that used the 'hack', must be updated :(Lisabethlisan
C
1

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.

Cida answered 26/11, 2015 at 7:12 Comment(0)
D
0

[[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?

Devinne answered 21/8, 2012 at 10:10 Comment(0)
I
0

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);
Intrigue answered 19/7, 2014 at 21:49 Comment(0)
M
0

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;
}
Mckamey answered 2/12, 2014 at 19:10 Comment(0)
B
0

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;
Brittanybritte answered 26/3, 2019 at 10:55 Comment(0)
F
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;

}

How to use:

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))
Forelady answered 1/10, 2020 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.