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

7

没有使用XIB文件,计算loadView方法中视图大小的最佳实践是什么?

这是我的解决方案:

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

这段代码是否可以,还是需要我进行一些编辑?

3个回答

6
文档建议使用[[UIScreen mainScreen] applicationFrame]来获取不包括状态栏的屏幕边界。

谢谢,这里有文档供有兴趣的人参考:https://developer.apple.com/library/IOs/documentation/UIKit/Reference/UIScreen_Class/Reference/UIScreen.html#//apple_ref/occ/instp/UIScreen/applicationFrame - CarlJ
i can我可以 - wattson12
你这个说法的依据是什么?你有参考资料吗? - lhunath

1

所以,对于任何想要了解最佳实践示例的人:

#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
}

0

你正在根据状态栏和导航栏调整高度。但是你还没有对视图的起点做任何处理。


是的,原点始终为{0, 0}。如果导航栏可见,则点0,0位于导航栏下方。或者我错了吗? - CarlJ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接