在loadView方法中计算视图大小的最佳实践

Car*_*rlJ 7 uiviewcontroller loadview ios

loadViewUIViewController没有XIB文件的情况下计算方法中的视图大小的最佳做法是什么?

这是我的解决方案:

- (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];      
}
Run Code Online (Sandbox Code Playgroud)

这段代码好吗,还是我应该编辑一些东西?

wat*_*n12 6

文档建议[[UIScreen mainScreen] applicationFrame]用于获取没有状态栏的屏幕边界