title: Objective C : Getting the height and width of a UIView accurately
body: <p>I having problems getting an accurate reading for heights and widths. So I made this quick and dirty app to test the situation.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>@interface wwfpViewController ()
  @property (nonatomic, strong) UIView * box;
  @property (nonatomic, strong) UILabel * info;
  @end
  
  @implementation wwfpViewController
  @synthesize box,info;
  
  - (void)viewDidLoad {
      [super viewDidLoad];
  
      box=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
      [box setBackgroundColor:[UIColor darkGrayColor]];
      [box setAutoresizesSubviews:YES];
      [box setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
  
      info=[[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 300)];
      [info setTextColor:[UIColor whiteColor]];
      [info setBackgroundColor:[UIColor blackColor]];
      [info setLineBreakMode:NSLineBreakByWordWrapping];
      [info setNumberOfLines:10];
      [info setText:@"..."];
  
      [self.view addSubview:box];
      [box addSubview:info];
  
      [self updateInfo];
  
      [[NSNotificationCenter defaultCenter] addObserver:self
                                               selector:@selector(updateView:)
                                                   name:UIApplicationDidChangeStatusBarOrientationNotification
                                                 object:nil];
  
  }
  
  - (void) updateInfo {
      CGFloat selfHeight=self.view.frame.size.height;
      CGFloat selfWidth=self.view.frame.size.width;
      CGFloat boxHeight=box.frame.size.height;
      CGFloat boxWidth=box.frame.size.width;
      int deviceOrientation=[[UIDevice currentDevice] orientation];
      int statusOrientation=[[UIApplication sharedApplication] statusBarOrientation];
  
      NSString * str=[NSString stringWithFormat:@"[height x width] \nself: [%f x %f] \nbox: [%f x %f] \ndevice: %d status: %d",selfHeight,selfWidth,boxHeight,boxWidth,deviceOrientation,statusOrientation];
  
      [info setText:str];
  }
  
  - (void) updateView: (NSNotification*) notify {
      [self updateInfo];
  }
  
  
  @end
  </code></pre>
  
  <p>When I test this on an iPad, initially in portrait mode, the info label reports the following:</p>
  
  <pre><code>[height x width]
  self: [1004.000000 x 768.000000]
  box: [1004.000000 x 768.000000]
  device: 0 status: 1
  </code></pre>
  
  <p><strong>This is correct!</strong></p>
  
  <p>And then when I rotate the iPad to landscape, I get these readings:</p>
  
  <pre><code>[height x width]
  self: [768.000000 x 1004.000000]
  box: [1004.000000 x 768.000000]
  device: 3 status: 3
  </code></pre>
  
  <p><strong>Actual height x width: 748 x 1024</strong></p>
  
  <p>But when I test this on the iPad when its in landscape orientation, the info label reports:</p>
  
  <pre><code>[height x width]
  self: [1024.000000 x 748.000000]
  box: [1024.000000 x 748.000000]
  device: 0 status: 3
  </code></pre>
  
  <p><strong>Actual height x width: 748 x 1024</strong></p>
  
  <p>Then when I rotate the iPad to portrait, I get these readings:</p>
  
  <pre><code>[height x width]
  self: [748.000000 x 1024.000000]
  box: [748.000000 x 1024.000000]
  device: 1 status: 1
  </code></pre>
  
  <p><strong>Actual height x width: 1004 x 768</strong></p>
  
  <p>I rotate it back to landscape and then I get these readings:</p>
  
  <pre><code>[height x width]
  self: [768.000000 x 1004.000000]
  box: [1004.000000 x 768.000000]
  device: 3 status: 3
  </code></pre>
  
  <p><strong>Actual height x width: 748 x 1024</strong></p>
  
  <p>In all cases the <code>box</code> UIView covers the entire screen, so it is auto adjusting to the orientation changes correctly. These results are consistent from the simulator and testing it on an actual iPad, and I have similar experiences on an iPhone.</p>
  
  <p>After this, I have a few questions:</p>
  
  <ol>
  <li>What am I doing wrong?</li>
  <li>Why is the height and width for <code>self.view</code> different from the height and width for <code>box</code> when the two look visually identical?</li>
  <li><h1><strong>How can I accurately obtain the overall height and width of the screen or a view, irrespective of orientation changes.</strong></h1></li>
  <li>Because <code>[UIDevice ... orientation]</code> reports as zero the first time it is used, should I just ignore it altogether and just stick with <code>[UIApplication ... statusBarOrientation]</code>?</li>
  </ol>