Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to calculate view size within loadView method

What is the best practice to calculate the view size in the loadView method (in an UIViewController) without a XIB file?

Here is my solution:

- (void)loadView {

  //Calculate Screensize
  BOOL statusBarHidden = [[UIApplication sharedApplication] isStatusBarHidden ];
  BOOL navigationBarHidden = [self.navigationController isNavigationBarHidden];
  BOOL tabBarHidden = [self.tabBarController.tabBar isHidden];

  CGRect frame = [[UIScreen mainScreen] bounds];

  if (!statusBarHidden) {
    frame.size.height -= [[UIApplication sharedApplication] statusBarFrame].size.height; 
  }
  if (!navigationBarHidden) {
    frame.size.height -= self.navigationController.navigationBar.frame.size.height; 
  }
  if (!tabBarHidden) {
    frame.size.height -= self.tabBarController.tabBar.frame.size.height; 
  }

  UIView *v = [[UIView alloc] initWithFrame: frame];
  [v setBackgroundColor: [UIColor whiteColor] ];
  [v setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];
  [self setView: v ];
  [v release];      
}

Is this code okay, or should I edit something?

like image 254
CarlJ Avatar asked Feb 06 '12 09:02

CarlJ


1 Answers

The docs recommend using [[UIScreen mainScreen] applicationFrame] to get the screen bounds without the status bar

like image 175
wattson12 Avatar answered Sep 22 '22 20:09

wattson12