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

发布于 2025-01-03 00:23:55 字数 1075 浏览 1 评论 0原文

在没有 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];      
}

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

世俗缘 2025-01-10 00:23:55

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

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

辞旧 2025-01-10 00:23:55

因此,对于任何想了解最佳实践示例的人:

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

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
}
晨曦÷微暖 2025-01-10 00:23:55

您正在根据状态栏和导航栏调整高度。但是您没有对视图的起源做任何事情。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文