iOS: enhancement for status bar height retrieval

This commit is contained in:
王劲鹏 2023-03-01 15:05:01 +08:00 committed by osborn
parent 63f8b87a1b
commit 15364618e1
3 changed files with 59 additions and 1 deletions

View File

@ -103,7 +103,7 @@ - (instancetype)init {
@"screenWidth": @(screenWidth), @"screenWidth": @(screenWidth),
@"screenHeight": @(screenHeight), @"screenHeight": @(screenHeight),
@"screenScale": @([[UIScreen mainScreen] scale]), @"screenScale": @([[UIScreen mainScreen] scale]),
@"statusBarHeight": @([[UIApplication sharedApplication] statusBarFrame].size.height), @"statusBarHeight": @(systemStatusBarHeight()),
@"hasNotch": @(hasNotch()), @"hasNotch": @(hasNotch()),
@"deviceBrand": @"Apple", @"deviceBrand": @"Apple",
@"deviceModel": platform, @"deviceModel": platform,

View File

@ -54,6 +54,14 @@ UIImage *_Nonnull UIImageWithColor(UIColor *_Nonnull color);
BOOL hasNotch(void); BOOL hasNotch(void);
CGFloat systemStatusBarHeight(void);
CGFloat statusBarHeightFixed(void);
BOOL isDynamicIslandDevice(void);
BOOL isX(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -23,6 +23,7 @@
#import "DoricUtil.h" #import "DoricUtil.h"
#import "DoricContext.h" #import "DoricContext.h"
#import "UIView+Doric.h" #import "UIView+Doric.h"
#import <sys/utsname.h>
void DoricLog(NSString *_Nonnull format, ...) { void DoricLog(NSString *_Nonnull format, ...) {
va_list args; va_list args;
@ -141,3 +142,52 @@ BOOL hasNotch() {
return NO; return NO;
} }
} }
CGFloat systemStatusBarHeight() {
CGFloat statusBarHeight = 0;
if (@available(iOS 13.0, *)) {
UIStatusBarManager *statusBarManager = [UIApplication sharedApplication].windows.firstObject.windowScene.statusBarManager;
statusBarHeight = statusBarManager.statusBarFrame.size.height;
} else {
statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
}
statusBarHeight = statusBarHeight > 0 ? statusBarHeight : statusBarHeightFixed();
return statusBarHeight;
}
CGFloat statusBarHeightFixed() {
if (isDynamicIslandDevice()) {
return 54.0f;
}
return isX() ? 44.0f : 20.0f;
}
BOOL isDynamicIslandDevice() {
struct utsname systemInfo;
uname(&systemInfo);
NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];
if (TARGET_OS_SIMULATOR == 1) {
platform = [NSProcessInfo new].environment[@"SIMULATOR_MODEL_IDENTIFIER"];
}
if ([platform isEqualToString:@"iPhone15,2"] || [platform isEqualToString:@"iPhone15,3"]) {
return YES;
} else {
return NO;
}
}
BOOL isX() {
BOOL iPhoneXSeries = NO;
if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
return iPhoneXSeries;
}
if (@available(iOS 11.0, *)) {
UIWindow *mainWindow = [UIApplication sharedApplication].windows.firstObject;
if (mainWindow.safeAreaInsets.bottom > 0) {
iPhoneXSeries = YES;
}
}
return iPhoneXSeries;
}