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

在没有XIB文件的情况下,在loadView方法(在UIViewController中)计算视图大小的最佳实践是什么?

这是我的解决方案:

- (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];      
}

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

解决方法

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...