Best practice for calculating view size in loadView method

What is the best practice for calculating the view size in the loadView method (in a UIViewController ) without an 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 ok or do I need to edit something?

+7
source share
3 answers

Documents recommend using [[UIScreen mainScreen] applicationFrame] to get screen borders without status bar

+6
source

therefore, for those who want to learn an example of best practice:

 #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, eg 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 } 
+1
source

You adjust the height depending on the status bar and navigation bars. But you did nothing in terms of the origin of the submission.

0
source

All Articles