S K
S K

Reputation: 91

iOS 16 status bar height is getting wrong on iPhone 14 series

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

Answers (2)

Pix1O
Pix1O

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

Sommm
Sommm

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

Related Questions