Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

swift - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...