1

Since setStatusBarHidden is deprecated, we will be using preferredStatusBarHidden in UIViewControllers. But, how do I hide and show easily in a object class or custom UITableviewCell class. We used to hide show using UIApplication like this

[[UIApplication sharedApplication] setStatusBarHidden:YES];

Now it is deprecated and it can be used only in UIViewControllers, is there a way to hide them in custom tableview cells and NSObject classes?

nOOb iOS
  • 1,384
  • 2
  • 19
  • 37
  • As the status bar display is controlled by the UIViewController what is the scenario where you require to execute the code on anything other than a UIViewController? – Adam Richardson Oct 02 '18 at 16:09
  • @nOOb_iOS if my answer works for you please mark it as right answer. Thanks – Faiz Fareed Oct 16 '18 at 17:42

2 Answers2

1

prefersStatusBarHidden is a method of UIViewController objects, not UIApplication. So you should set it on UIViewController. Like this.

- (BOOL)prefersStatusBarHidden {

   return NO;
}

Note:

  • If you change the return value for this method, call the setNeedsStatusBarAppearanceUpdate method.
  • For childViewController, To specify that a child view controller should control preferred status bar hidden/unhidden state, implement the childViewControllerForStatusBarHidden method.

if you want to more dynamically hide or show the status bar

You can apply logic like this,

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { shouldHideStatusBar = (shouldHideStatusBar)? NO: YES; [self setNeedsStatusBarAppearanceUpdate]; }

You can add this code to your button event as well

make sure to add to your info.plist "View controller-based status bar appearance" set to YES otherwise things just don't seem to work.

Faiz Fareed
  • 1,498
  • 1
  • 14
  • 31
0

Basically all you need is a Base VC that you will be subclassing in the VC's you want to control the status bar. In there you will be subscribing for a given Notification, which when sent will control the status bar of the VC.

Here is the code:

extension Notification.Name {
    static var statusBarShowHide: Notification.Name {
        return .init(rawValue: "StatusBar.showHide")
    }
}

class StatusBarControllableVC : UIViewController {

    private var statusBarHidden = false

    private func topViewController() -> UIViewController? {
        var topController = UIApplication.shared.keyWindow?.rootViewController
        while topController?.presentedViewController != nil {
            topController = topController?.presentedViewController
        }
        return topController
    }

    override var prefersStatusBarHidden: Bool {
        return statusBarHidden
    }

    @objc func statusBarShowHide(notification: NSNotification) {
        guard let userInfo = notification.userInfo else {return}
        let topController = topViewController()

        if let hidden = userInfo["hidden"] as? Bool, self == topController {
            statusBarHidden = hidden
            self.setNeedsStatusBarAppearanceUpdate()
        }
    }

    override func viewDidLoad() {
        NotificationCenter.default.addObserver(self, selector: #selector(statusBarShowHide(notification:)), name: .statusBarShowHide, object: nil)
        super.viewDidLoad()
    }
}

Now whenever you need to show or hide the Status Bar, you will post a Notification like this:

NotificationCenter.default.post(name: .statusBarShowHide, object: nil, userInfo: ["hidden":true])

I believe this is the simplest approach. In the code above I'm only showing/hiding the Status Bar on the Top Most View Controller, but obviously you can change that

Lefteris
  • 14,550
  • 2
  • 56
  • 95