Search code examples
iosstatusbar

How to set the status bar height to 20pt when on a call like the App Store does?


When on a phone call while in the iOS App Store the status bar changes from 40pt tall to 20pt tall when reading an article. How do I do this in my app?enter image description here


Solution

  • this is really an interesting question, took me sometimes to figure it out.

    1. if you notice app store in such viewcontroller didn't have a status bar, which means the green bar should not be shown at all. this is the first hint.

    2. you can access the status bar by UIApplication.shared.value(forKey: "statusBarWindow") as! UIWindow

    3. according to point number one this means the status bar is not hidden but actually its statusBarWindow.frame.origin.y just shifted up.

    4. please not you need to handle iPhone x separately

    5. please make sure the status bar is not hidden

    6. note that this is not the only right way, right now you have the view itself, you can change the origin or size or even try to get what inside this view and hide them or change their frames too etc.

    here is an example how you can do it.

    class ViewController: UIViewController {
    
    var isHidden:Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        UIView.animate(withDuration: 0.5) { () -> Void in
            let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as! UIWindow
            statusBarWindow.frame = CGRect(x: 0, y: -20, width: statusBarWindow.frame.size.width, height: 40.0)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override var prefersStatusBarHidden: Bool{
        return isHidden
    }
    

    }

    also attached an image with the result of this code, hopefully, this answer work for you too.

    enter image description here