Status Bar is pushing down view content, only when app is launched with In-call status bar activated

Viewed 1026

So I have some weird behaviour here. I have a basic iOS app coded in Swift. It's using a WKWebView along with a few other small features.

One major problem at the moment is the 'In-call Status Bar'. If I toggle the in-call status bar while the application is open, it looks perfectly fine:

enter image description here


Although if I toggle the in-call status bar before I open the application and then run it, the layout goes all weird:

enter image description here


Along with toggling the status bar to 'off', it then goes even weirder (20px of white space at the top):

enter image description here


The issue was happening even when the in-call status bar was toggled while the application was open, although I fixed this (hence the first image looking fine) with this simple one-liner:

webView.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]

How can I get my webview to accomodate for this, even when the application is open while a call is activated?

1 Answers

I've come across this problem too, and solved it by checking the size of the status bar when the app opens, and if it is 40 points then listening for UIApplicationDidChangeStatusBarFrame notifications, and when one occurs, moving the views frame to .zero.

For example, in Swift 4:

var sbshown: Bool = false

override func viewDidLoad() {
    super.viewDidLoad()

    //check height of status bar when app opens, and set a boolean if open
    let sbheight = UIApplication.shared.statusBarFrame.height

    NSLog("status bar height %f", sbheight)

    if (sbheight == 40) {
        sbshown = true
    }

    //set up to receive notifications when the status bar changes
    let nc = NotificationCenter.default 

    nc.addObserver(forName: NSNotification.Name.UIApplicationDidChangeStatusBarFrame, object: nil, queue: nil, using: statusbarChange)

}

Then implement a method to resize and reposition the view when the status bar disappears:

func statusbarChange(notif: Notification) -> Void {

     if (sbshown) {
         sbshown = false

         self.view.frame.origin = .zero
         self.view.frame.size.height = UIScreen.main.bounds.height
     }    

}

So, the repositioning should only happen if the bar was already shown when the app opened, not if the bar is shown and hidden while the app is already open.

Related