7

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?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
CarlJ
  • 9,461
  • 3
  • 33
  • 47

3 Answers3

6

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

wattson12
  • 11,176
  • 2
  • 32
  • 34
  • thx, here are the docs for anyone who is interested: https://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIScreen_Class/Reference/UIScreen.html#//apple_ref/occ/instp/UIScreen/applicationFrame – CarlJ Feb 06 '12 at 10:55
  • [i can](https://developer.apple.com/library/IOs/#documentation/UIKit/Reference/UIScreen_Class/Reference/UIScreen.html) – wattson12 Feb 06 '12 at 10:56
  • What do you base this claim on? Do you have a reference? – lhunath Nov 16 '17 at 20:32
1

so for anyone who want to know a best practice example:

#pragma mark -
#pragma mark LoadView Methods
- (void)loadView {

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

  CGRect frame = [[UIScreen mainScreen] applicationFrame];

  //check if you should rotate the view, e.g. change width and height of the frame
  BOOL rotate = NO;
  if ( UIInterfaceOrientationIsLandscape( [UIApplication sharedApplication].statusBarOrientation ) ) {
    if (frame.size.width < frame.size.height) {
      rotate = YES;
    }
  }

  if ( UIInterfaceOrientationIsPortrait( [UIApplication sharedApplication].statusBarOrientation ) ) {
    if (frame.size.width > frame.size.height) {
      rotate = YES;
    }
  }

  if (rotate) {
    CGFloat tmp = frame.size.height;
    frame.size.height = frame.size.width;
    frame.size.width = tmp;
  }


  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;
  }
  if (!toolBarHidden) {
    frame.size.height -= self.navigationController.toolbar.frame.size.height;
  }

  UIView *v = [[UIView alloc] initWithFrame: frame];
  v.backgroundColor = [UIColor whiteColor];
  v.autoresizingMask  = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

  self.view = v;
  [v release]; //depends on your ARC configuration
}
CarlJ
  • 9,461
  • 3
  • 33
  • 47
0

You are adjusting the height depend on the status bar and navigation bars.. But you have not done anything with respect to the origin of the view.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
  • yes the origin is always {0, 0}. An if the navBar is visible the point 0,0 is under the navBar. or I'm wrong? – CarlJ Feb 06 '12 at 10:10