swiftios-statusbar

I am not able to hide statusBar when I try to show a view


I want to hide statusBar when I show a view in screen.

func showView() {

    if let keyWindow = UIApplication.shared.keyWindow{
        let view = UIView(frame: keyWindow.frame)
        view.backgroundColor = UIColor.black
        keyWindow.addSubview(view)

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            view.frame = keyWindow.frame
        }) { (completedAnimnation) in
            //hide status bar when view is showed
            UIApplication.shared.isStatusBarHidden = true
        }
    }

}

This is the code that I show the view and I try to hide statusBar using : UIApplication.shared.isStatusBarHidden = true . and also UIApplication.shared.setStatusBarHidden(true, with: .fade) but none of these are working. Also can not override prefersStatusBarHidden because I am on a UIView class.

override var prefersStatusBarHidden: Bool {
    return true
}

Note: Please, do not mark as duplicate because I have seen all other answers but none of them are working.I don't want to hide for all application, only when It shows the view.


Solution

  • step 1: In Info.plist file Click the plus button to add new key View controller-based status bar appearance Set the VALUE to NO

    step 2: For hide in a single screen

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        UIApplication.shared.isStatusBarHidden = true
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    
        UIApplication.shared.isStatusBarHidden = false
    }
    

    step 3: For hide in entire app AppDelegate.swift

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject:AnyObject]?) -> Bool {
        application.statusBarHidden = true
        return true
    }