85

Hi I'm wondering if there's a way to get the width programmatically.

I'm looking for something general enough to accomodate iphone 3gs, iphone 4, ipad. Also, the width should change based on if the device is portrait or landscape (for ipad).

Anybody know how to do this?? I've been looking for a while... thanks!

Shai UI
  • 50,568
  • 73
  • 204
  • 309
  • Is the width of the screen, the width of the available application space (without the system bar) or the width of a particular view your are looking for ? – VdesmedT Sep 07 '10 at 08:06
  • hrmm i'm not sure. i guess i just wanted the width of device (i.e in ipad 768 for portrait width and 1024 for landscape width) but self.view.bounds seemed to satisfy this. see my comment below – Shai UI Sep 07 '10 at 17:41
  • See [this more comprehensive answer](http://stackoverflow.com/a/7905540/17339) that takes into account the device's orientation. – Drew Stephens Apr 19 '12 at 22:52
  • @DrewStephens See my answer below which takes into account orientation _and_ is only 3 lines of code :D. – memmons Mar 29 '13 at 17:12

7 Answers7

212

Take a look at UIScreen.

eg.

CGFloat width = [UIScreen mainScreen].bounds.size.width;

Take a look at the applicationFrame property if you don't want the status bar included (won't affect the width).

UPDATE: It turns out UIScreen (-bounds or -applicationFrame) doesn't take into account the current interface orientation. A more correct approach would be to ask your UIView for its bounds -- assuming this UIView has been auto-rotated by it's View controller.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    CGFloat width = CGRectGetWidth(self.view.bounds);
}

If the view is not being auto-rotated by the View Controller then you will need to check the interface orientation to determine which part of the view bounds represents the 'width' and the 'height'. Note that the frame property will give you the rect of the view in the UIWindow's coordinate space which (by default) won't be taking the interface orientation into account.

Pang
  • 9,564
  • 146
  • 81
  • 122
Alan Rogers
  • 15,018
  • 3
  • 28
  • 19
  • 3
    actually, this doesn't work when the ipad is oriented landscape.. i.e., if my simulator is running landscape then it still returns 768 (instead of 1024). do you think i should have an if statement that checks orientation in that case or is there a better way to get width? – Shai UI Sep 07 '10 at 06:22
  • Damn --looks like you are correct; I've updated my answer with some more ideas. – Alan Rogers Sep 07 '10 at 07:17
  • I ended up just using "CGFloat width = CGRectGetWidth(self.view.bounds);" in my method (no need for didRotateFromInterfaceOrientation)... so atleast self.view.bounds takes into account orientation. thanks!! – Shai UI Sep 07 '10 at 17:36
  • 5
    for others who may have stumbled upon this older post, I found that self.view.bounds is only entirely accurate starting in didRotateFromInterfaceOrientation. It will also be accurate in viewDidLoad and viewWillAppear if the view is shown in its initial target orientation (as specified in IB). If the view is shown in a different orientation then targeted, viewDidLoad (and viewWillAppear) is called before didRotateFromInterfaceOrientation and view.bounds will be incorrect – rimsky Jan 30 '12 at 19:43
  • In case of iOS 6, and when we use Modal controllers, the didRotateFromInterfaceOrientation will not be called – RK- Dec 26 '12 at 15:26
  • 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. – Bobby Feb 28 '16 at 22:46
12
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
//Bonus height.
CGFloat height = CGRectGetHeight(screen);
6

This can be done in in 3 lines of code:

// grab the window frame and adjust it for orientation
UIView *rootView = [[[UIApplication sharedApplication] keyWindow] 
                                   rootViewController].view;
CGRect originalFrame = [[UIScreen mainScreen] bounds];
CGRect adjustedFrame = [rootView convertRect:originalFrame fromView:nil];
memmons
  • 40,222
  • 21
  • 149
  • 183
  • 3
    this is close, but if a UIAlertView is being displayed it will be the key window and its rootViewController property will be nil – matt bezark Apr 02 '13 at 21:04
  • @mattbezark True. I consider that a corner case though. There is an alternative to get the root view -- not as clean as this one, but I'll post the alternative in case anyone is interested. – memmons Apr 03 '13 at 14:39
  • @memmons a little late to the party but I'd be interested in your alternate solution :) – alexgophermix Oct 16 '15 at 16:59
1

use:

NSLog(@"%f",[[UIScreen mainScreen] bounds].size.width) ;
Nick
  • 875
  • 6
  • 20
0

As of iOS 9.0 there's no way to get the orientation reliably. This is the code I used for an app I design for only portrait mode, so if the app is opened in landscape mode it will still be accurate:

screenHeight = [[UIScreen mainScreen] bounds].size.height;
screenWidth = [[UIScreen mainScreen] bounds].size.width;
if (screenWidth > screenHeight) {
    float tempHeight = screenWidth;
    screenWidth = screenHeight;
    screenHeight = tempHeight;
}
Bobby
  • 6,115
  • 4
  • 35
  • 36
0

Use this code it will help

[[UIScreen mainScreen] bounds].size.height
[[UIScreen mainScreen] bounds].size.width
Shahzaib Maqbool
  • 1,479
  • 16
  • 25
-2

Here is a Swift way to get screen sizes, this also takes current interface orientation into account:

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 in:

https://github.com/goktugyil/EZSwiftExtensions

Esqarrouth
  • 38,543
  • 21
  • 161
  • 168