iOS adapts to the height change of the top of safeArea of Smart Island

After coming out of Smart Island, I found a white bar on the top of the online iOS application

insert image description here

Our application is made by nesting wkwebview. The code for modifying the color of the top status bar before is like this

- (void) changeStatusBarColor: (NSString *) hexColor {
    if (@available(iOS 13.0, *)) {
        // iOS 13  弃用keyWindow属性  从所有windowl数组中取
        UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
        statusBar.backgroundColor = [self colorWithHexString:hexColor];
        [[UIApplication sharedApplication].keyWindow addSubview:statusBar];
    }else{
        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
            statusBar.backgroundColor = [self colorWithHexString:hexColor];
        }
    }
}

After checking, it was found that the height of the statusbar of the Smart Island model system is inconsistent with the height of the top of the safeArea. The notch model has the same statusbar and safeArea at 47, the Smart Island model has a statusbar height of 54, and the height of the safeArea top is 59, resulting in a white border with a height of 5.

Therefore, the adaptation solution is to modify the height of the statusbar to be consistent with the height of the top of the safeArea. The code is as follows

- (void) changeStatusBarColor: (NSString *) hexColor {
    if (@available(iOS 13.0, *)) {
        // iOS 13  弃用keyWindow属性  从所有windowl数组中取
        UIView *statusBar = [[UIView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.windowScene.statusBarManager.statusBarFrame] ;
        statusBar.backgroundColor = [self colorWithHexString:hexColor];
        // 修改statusBar高度适配灵动岛
        [statusBar setFrame:CGRectMake(statusBar.frame.origin.x, statusBar.frame.origin.y, statusBar.frame.size.width, [UIApplication sharedApplication].keyWindow.safeAreaInsets.top)];
        [[UIApplication sharedApplication].keyWindow addSubview:statusBar];
    }else{
        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
            statusBar.backgroundColor = [self colorWithHexString:hexColor];
        }
    }
}

effect achieved

insert image description here

Guess you like

Origin blog.csdn.net/ouchangjian/article/details/129258835