gpt4 book ai didi

ios - 在 loadView 方法中计算 View 大小的最佳实践

转载 作者:可可西里 更新时间:2023-11-01 03:30:52 27 4
gpt4 key购买 nike

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

这是我的解决方案:

- (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] 获取没有状态栏的屏幕边界

关于ios - 在 loadView 方法中计算 View 大小的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9158315/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com