Reputation: 91
I have tried this solution to get status bar height on iPhone 14 series with iOS 16 but getting wrong size. In app scene delegate is not used.
Upvotes: 0
Views: 4206
Reputation: 21
+ (CGFloat)statusBarHeight {
if ( @available(iOS 13.0, *)) {
NSSet *set = [UIApplication sharedApplication].connectedScenes;
UIWindowScene *windowScene = [set anyObject];
UIStatusBarManager *statusBarManager = windowScene.statusBarManager;
CGFloat statusBarHeight = statusBarManager.statusBarFrame.size.height;
if( @available(iOS 16.0, *)) {
BOOL needAdjust = (statusBarHeight == 44);
if(windowScene.windows.firstObject.safeAreaInsets.top == 59 && needAdjust) {
statusBarHeight = 59;
}
}
return statusBarHeight;
}
CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;
CGFloat safeAreaTop = UIApplication.sharedApplication.windows.firstObject.safeAreaInsets.top;
return MAX(statusBarHeight, safeAreaTop);
}
Upvotes: 2
Reputation: 531
Please try the below solution. Working for iPhone 14 Pro and iPhone 14 max Pro.
if #available(iOS 13.0, *) {
let window = UIApplication.shared.windows.first
let topPadding = window?.safeAreaInsets.top
let statusBar = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: topPadding ?? 0.0))
statusBar.backgroundColor = UIColor(named: "AppPrimaryColor")
UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.addSubview(statusBar)
}
Hope this will solve your issue. Keep coding :)
Upvotes: 0