使用新的API 【statusBarManager】
if ([UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom > 0)
[Bugly] Trapped uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
keyWindow.windowScene.statusBarManager
if (@available(iOS 13.0, *)) {
UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
statusBar.backgroundColor = color;
[[UIApplication sharedApplication].keyWindow addSubview:statusBar];
} else {
// Fallback on earlier versions
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
statusBarFrame
CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
//API_DEPRECATED("Use the statusBarManager property of the window scene instead.", ios(2.0, 13.0));
if (@available(iOS 13.0, *)) {
rectStatus = [UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame;
}
1、iOS 13之前,可以通过valueForKey 获取UIApplication的statusBar,因为UIApplication是单例,因此,在iOS 12,通过:
[[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]
拿到的statusBar永远是同一个对象。2、iOS 13之后,因为苹果不允许使用KVC的valueForKey访问私有属性。通过上面的代码获取statusBar时,发现每次每次获取都调用 alloc:init的方法,重新生成一个statusBar;然后添加到UIApplication的keyWindow上,再设置背景颜色。因此这个方法多次调用就会创建多份statusBar,造成内存开销不说,想设置状态栏为为透明,根本没效果。
解决办法:既然定位到问题所在,办法就是保证iOS 13 之后,每次也都能拿到有去只有一个对象。方法有很多,我的方法代码如下:使用 static 配合 gcd
HSSingletonM(QCTStatusBarTool);
+ (void)setStatusBarBackgroundColor4ios13:(UIColor *)color {
static UIView *statusBar = nil;
static dispatch_once_t onceToken;
if (@available(iOS 13.0, *)) { //iOS 13不允许使用valueForKey、setValue: forKey获取和设置私有属性;
dispatch_once(&onceToken, ^{
statusBar = [[UIView alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
[[UIApplication sharedApplication].keyWindow addSubview:statusBar];
});
} else {
statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
}
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
#define isIphoneX [QCT_Common isiPhoneX]
+ (BOOL)isiPhoneX {
if (@available(iOS 11.0, *)) {
if ([UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom > 0) {//iphoneX横屏
return YES;
} else {
return NO;
}
} else {
return NO;
}
}
///*状态栏和导航栏总高度*/
#define kStatusBarHeight (CGFloat)(isIphoneX?(88.0):(64.0))
/*iPhoneX的状态栏高度差值*/
#define kPtatusBarHeight (CGFloat)(isIphoneX?(24.0):(0))
/*底部安全区域远离高度*/
#define kDtatusBarHeight (CGFloat)(isIphoneX?(34.0):(0))
导航栏显示公告和标题
navView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kStatusBarHeight)];
/**
用于设置状态栏的背景图片
*/
@property (weak, nonatomic) UIButton *imgLable;
- (UIButton *)imgLable{
if (nil == _imgLable) {
UIButton *tmpView = [[UIButton alloc]init];
_imgLable = tmpView;
tmpView.userInteractionEnabled = NO;
[tmpView setBackgroundImage:[UIImage imageNamed:@"img_daiban_topbg_top"] forState:UIControlStateNormal];
[self addSubview:_imgLable];
__weak __typeof__(self) weakSelf = self;
[_imgLable mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(weakSelf).offset(0);
make.top.equalTo(weakSelf).offset(0);
make.height.mas_equalTo(kStatusBarHeight);
// make.top.equalTo.offset(kAdjustRatio(44));
}];
}
return _imgLable;
}
推荐使用自动布局,和使用API获取高度
#define kstatusBarH [[UIApplication sharedApplication] statusBarFrame].size.height
#define knavHeight self.navigationController.navigationBar.frame.size.height
//获取状态栏的高度
CGFloat statusHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
NSLog(@"状态栏高度:%f",statusHeight);
//获取导航栏的高度
CGFloat navHeight = self.navigationController.navigationBar.frame.size.height;
NSLog(@"导航栏高度:%f",navHeight);
//获取tabBar的高度
//1.在tabBarController中使用(你的继承自UITabBarController的VC)
CGFloat tabBarHeight = self.tabBar.frame.size.height;
NSLog(@"tabBar高度:%f",tabBarHeight);
//2.在非tabBarController中使用
UITabBarController *tabBarVC = [[UITabBarController alloc] init];//(这儿取你当前tabBarVC的实例)
CGFloat tabBarHeight = tabBarVC.tabBar.frame.size.height;
NSLog(@"tabBar高度:%f",tabBarHeight);