-4

Inside the viewDidLoad method of a UIViewController:

NSLog(@"frame: %f %f", self.view.frame.size.width, self.view.frame.size.height);

With the device in landscape:

frame: 748.000000 1024.000000

With the device in portrait:

frame: 768.000000 1004.000000
Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
Andrew
  • 15,935
  • 28
  • 121
  • 203

5 Answers5

13

In viewDidLoad, the frame may not have adjusted to all of the things that affect its final size, such as rotation, status bar height changes, other views, etc. It's generally best to only create and set up your objects here; you can inspect the finalized frame in viewWillLayoutSubviews.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • 1
    `viewWillLayoutSubviews` is called every time the view rotates. Rotation is my next problem, and setting up the frame there causes it to jump, unlike the smooth autorotation that safari has. – Andrew May 07 '13 at 14:11
  • You can also animate the changes in `willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation` to avoid that jumping. – Aaron Brager May 07 '13 at 14:15
5

There is no size of the a view controller. Its the size of the view or the window. you can use the .frame or .bounds method to get it. Also you can easily print the rect using,

NSLog("Frame: %@", NSStringFromCGRect(self.view.frame));
NSLog("Frame: %@", NSStringFromCGRect(self.view.window.frame));

You can also use the [UIScreen mainScreen].bounds to get the size of the full screen. You will always get that 20 pixel offset in x or y direction, for the status bar, depending on your orientation.

Whatever happens, the frame returns the height and width, assuming the device is in portrait mode, with the home button down. So when the device is in landscape mode, the status bar is on the right side, and the width is 20 pixel less to make room for the status bar.

Edited: After iOS 8 Now you get the width and height correctly in portrait and landscape orientation. i.e. as it looks -- in landscape width=portrait height. When I use in my code,

CGRect frame = [[UIScreen mainScreen] applicationFrame];
    CGSize size = frame.size;
    if (![UIUtil isAfterIOS8]){
        size.width = frame.size.height;
        size.height = frame.size.width;
    }

/* UIUtil.m */ 
+ (BOOL) isAfterIOS8 {
    return [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0;
}
karim
  • 15,408
  • 7
  • 58
  • 96
  • Note the "width" of 748 with the device in landscape. This is one of his issues. – Aaron Brager May 07 '13 at 14:15
  • Whatever happens, the frame returns the height and width, assuming the device is in portrait mode, with the home button down. So when the device is in landscape mode, the status bar is on the right side, and the width is 20 pixel less to make room for the status bar. – karim May 07 '13 at 14:17
  • Right, but why rotate everything 90º in your head when you can just wait for the frame to be set up properly/ – Aaron Brager May 07 '13 at 14:23
3

You should use self.view.bounds instead.

Toam
  • 362
  • 1
  • 5
1

You can log it as :

NSLog(@"My view's frame is: %@", NSStringFromCGRect(myView.frame));

Or try this

CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);

Or

CGFloat height = [self.view frame].size.height;
CGFloat width = [self.view frame].size.width;
Shardul
  • 4,266
  • 3
  • 32
  • 50
0

That is the current size of the main view in your view controller. The views are measured in points.

Note that the size of the iPad screen in points is 1024x768, but the size you are getting in the NSLog is without the status bar on top, which is 20 points tall.

kmie429
  • 36
  • 4
  • 2
    Note the "width" of 748 with the device in landscape. This is one of his issues. Also, the views are measured in points, not pixels. – Aaron Brager May 07 '13 at 14:14