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

2019-06-09 14:35发布

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.

2条回答
Anthone
2楼-- · 2019-06-09 14:50

My suggestion would be to create a variable:

var isVisible: Bool?

Set it to true when your desired UIView appears, and false when it disappears.

Alternatively, just use a ViewController, and present the view from there -

查看更多
劳资没心,怎么记你
3楼-- · 2019-06-09 14:56

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
}
查看更多
登录 后发表回答