How to hide status bar of a single view controller in iOS 9?

后端 未结 15 1232
挽巷
挽巷 2020-12-13 06:15

In a ViewController, which I presented modally, I did this:

override func prefersStatusBarHidden() -> Bool {
    return true
}

This used

相关标签:
15条回答
  • 2020-12-13 06:46

    In your UIViewController:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        UIApplication.shared.isStatusBarHidden = true
    }
    
     override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        //It will show the status bar again after dismiss
        UIApplication.shared.isStatusBarHidden = false
    }
    
    override var prefersStatusBarHidden: Bool {
        return true
    }
    
    0 讨论(0)
  • 2020-12-13 06:47

    Hide Status Bar smoothly by just using UIAnimation and stored property.

    Swift 3+

      var statusBarState = false
        override var prefersStatusBarHidden: Bool{
            return statusBarState
        }
    

    And then in viewWillAppear

      override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            statusBarState = true
            UIView.animate(withDuration: 0.30) {
                self.setNeedsStatusBarAppearanceUpdate()
            }
        }
    

    0 讨论(0)
  • 2020-12-13 06:51

    You can use

    override public func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // hide status bar
        dispatch_async(dispatch_get_main_queue(), {
            if let window = UIApplication.sharedApplication().keyWindow {
                window.windowLevel = UIWindowLevelStatusBar + 1
            }
        })
    
    }
    
    override public func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        // Show status bar
        dispatch_async(dispatch_get_main_queue(), {
            if let window = UIApplication.sharedApplication().keyWindow {
                window.windowLevel = UIWindowLevelNormal
            }
        })
    
    }
    
    0 讨论(0)
  • 2020-12-13 06:51

    Use this code:

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

    if this code doesn't work, you need to add this key in the info.plist

    View controller-based status bar appearance - NO

    0 讨论(0)
  • 2020-12-13 06:51

    Complete solution for iOS 11 and Swift 4, giving you full control from your program.

    var statusBarHidden : Bool?
    
    override var prefersStatusBarHidden: Bool {
        get {
            if let status = statusBarHidden { return status } else { return false }
        }
        set(status) {
            statusBarHidden = status
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    

    Now you can simply show or hide the status bar by setting the property from your code. I tested it like this:

    @IBAction func Show(_ sender: Any) {
        prefersStatusBarHidden = false
    }
    
    @IBAction func Hide(_ sender: Any) {
        prefersStatusBarHidden = true
    }
    

    Works like a charm.

    0 讨论(0)
  • 2020-12-13 06:53

    For those still struggling, the below works for iOS9.

    You update the rootViewController prefersStatusBarHidden function by calling it from your individual child/grandchild viewControllers. This works where you add childViewControllers directly to your rootViewController.

    You don't need to set anything in the info.plist, but setting setting 'statusBarIsInitiallyHidden' works independently of the below.

    First, in your rootViewController, add the following:

    -(void)viewDidLoad {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateStatusBarAppearance:) name:@"kStatusBarAppearance" object:nil]; 
    }
    -(void)updateStatusBarAppearance:(NSNotification *)n {
        statusBarIsHidden = [n.object boolValue];
        [self setNeedsStatusBarAppearanceUpdate];
    }
    -(UIStatusBarStyle)preferredStatusBarStyle {
        return UIStatusBarStyleLightContent; //optional
    }
    -(BOOL)prefersStatusBarHidden{
        return statusBarIsHidden;
    }
    

    Then, in the single view controller where you want to hide the status bar, call this:

    -(void)viewDidLoad {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kStatusBarAppearance" object:[NSNumber numberWithBool:true]];
    }
    -(void)popSelf {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kStatusBarAppearance" object:[NSNumber numberWithBool:false]];
    }
    
    0 讨论(0)
提交回复
热议问题