携手创造,共同成长!这是我参与「日新计划 8 月更文应战」的第32天,点击查看活动概况
引言
- 设置状况栏布景色彩的解决计划:
运用新的API 【statusBarManager】
- 经过安全区域高度判别是否IphoneX之后的机型:
if ([UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom > 0)
I 状况栏布景色彩的适配计划
- 问题
[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"];
1.1 运用新的API 【statusBarManager】
- 解决计划:运用
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.2 适配特征场景:状况是有通明或许半通明的作用的场景
-
弥补:如果整个项目的状况栏(电池栏)都是有色彩的,而且都是不通明的,上面这个办法完全可以应付。 但是如果有状况是有通明或许半通明的作用,上面这个办法仍是不能胜任,越通明越明显;经过一番查找,总算发现问题:
-
问题:
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;
}
}
II 经过安全区域高度判别是否IphoneX之后的机型
#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))
2.1 运用场景1:自定义导航栏内容
导航栏显现公告和标题
navView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kStatusBarHeight)];
2.2 运用场景2:设置状况栏布景图片
/**
用于设置状况栏的布景图片
*/
@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);
see also
- iOS13适配暗黑形式计划:1、设置app不支持Dark Mode 2、拟定深色形式开发标准 https://blog.csdn.net/z929118967/article/details/104293041