prefersStatusBarHidden not updating after calling setNeedsStatusBarAppearanceUpdate()


prefersStatusBarHidden not updating after calling setNeedsStatusBarAppearanceUpdate()



Different vcs inside my app show the status bar visible and others are hidden. This is set to YES in the info.pList


"View controller-based status bar appearance": YES

// also tried togging this between yes and no
"Status bar is initially hidden": YES



The app has 2 windows, the main window and a second window. The second window gets presented it front of the main window on a button push. The vc in the second window has the status bar hidden.



The problem is if I'm on a vc (mainVC) inside the main window that shows the status bar, I press the button to show the second window, mainVC's status bar disappears. The second window gets presented and after I dismiss it I send a notification to mainVC to call setNeedsStatusBarAppearanceUpdate() but prefersStatusBarHidden isn't triggered so the status bar stays hidden even though it shouldn't be. I even subclassed a Navigation Controller and added the code there with mainVC as it's root.


setNeedsStatusBarAppearanceUpdate()


prefersStatusBarHidden



Why isn't prefersStatusBarHidden getting called?


prefersStatusBarHidden



I added prefersStatusBarHidden inside the mainVC by itself, the nav to the mainVC by itself, and then in both the mainVC and it's nav at the same time. It's still not getting called after setNeedsStatusBarAppearanceUpdate() gets called in either places.


prefersStatusBarHidden


setNeedsStatusBarAppearanceUpdate()



Subclassed nav:


class MainVCNavController: UINavigationController {

override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)

NotificationCenter.default.addObserver(self, selector: #selector(updateStatusBar), name: NSNotification.Name(rawValue: "updateStatusBar"), object: nil)
}

let statusBarHidden: Bool = false

@objc func updateStatusBar() {
self.setNeedsStatusBarAppearanceUpdate() // this gets called when the notification is triggered
}

override var prefersStatusBarHidden: Bool {
return statusBarHidden // this doesn't get called after setNeedsStatusBarAppearanceUpdate() is called
}

// I added this just to see if it would make a difference but it didn't
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}

override open var childViewControllerForStatusBarStyle: UIViewController? {
return self.topViewController
}

override open var childViewControllerForStatusBarHidden: UIViewController? {
return self.topViewController
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
}



MainVC is the rootVC of the above nav


class MainVCController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(updateStatusBar), name: NSNotification.Name(rawValue: "updateStatusBar"), object: nil)
}

let statusBarHidden: Bool = false

@objc func updateStatusBar() {
self.setNeedsStatusBarAppearanceUpdate() // this gets called when the notification is called
}

override var prefersStatusBarHidden: Bool {
return statusBarHidden // this doesn't get called after setNeedsStatusBarAppearanceUpdate() is triggered
}

// I added this just to see if it would make a difference but it didn't
override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return .slide
}
}



SecondVC inside the second window has it's status bar hidden. It sends the notification when it's dismissed to the above mainVC:


class SecondController: UIViewController {

override var prefersStatusBarHidden: Bool {
return true
}

if dismissed {
NotificationCenter.default.post(name: Notification.Name(rawValue: "updateStatusBar"), object: nil)
}
}



I also read that I need to call the below to trigger the prefersStatusBarHidden but even when I added these to the updateStatusBar() it didn't make a difference.


prefersStatusBarHidden


updateStatusBar()


navigationController?.setNavigationBarHidden(false, animated: false)
// or
navigationController?.navigationBar.isHidden = false





You need to write prefersStatusBarHidden in the visible controller i.e. in the second controller to take action
– zombie
Jun 30 at 19:17


prefersStatusBarHidden





Do you mean the second window? The vc inside the second window, let's call it secondVC does have prefersStatusBarHidden set to true on it. It doesn't show the status bar. I'll update the question to show it
– Lance Samaria
Jun 30 at 19:20






to understand what I mean check this answer
– zombie
Jun 30 at 19:27





@zombie I actually read that this morning, thanks. After I read that I took it as the nav determines that status bar since it manages everything. That's when I subclassed the navVC in the question and added the prefersStatusBarHidden there but it didn't make a difference
– Lance Samaria
Jun 30 at 19:30





no use Symbolic Breakpoints
– zombie
Jun 30 at 19:58




2 Answers
2



Updating the status bar needs to be on the main thread.



There are two ways to ensure that:



Add notification observer on the main thread: (you don't need to expose the func to objc c):


NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "updateStatusBar"), object: nil, queue: .main, using: updateStatusBar)

func updateStatusBar(_ notification: Notification) {
setNeedsStatusBarAppearanceUpdate()
}



Or update the status bar on the main thread:


NotificationCenter.default.addObserver(self, selector: #selector(updateStatusBar(_:)), name: NSNotification.Name(rawValue: "updateStatusBar"), object: nil)

@objc func updateStatusBar(_ notification: Notification) {

DispatchQueue.main.sync {
self.setNeedsStatusBarAppearanceUpdate()
}
}





Hey man thanks. I only put the answer there because I wasn’t sure if you was going to follow up. I’ll delete mines.
– Lance Samaria
2 days ago






@LanceSamaria by the way if you remove the observer in deinit then you don't need the weak self
– zombie
2 days ago


deinit


weak self





Ever since Apple announced that there’s no need to remove observers anymore I’m used to just creating them and that’s it. But I also didn’t know what you just said. Do you want hear something interesting about your answer. I only listed 1 vc (mainVC) as showing the status bar and having the problem there. I have several others in different tanks that also show it. I don’t have notifications or prefersStatusBarHidden on those but if I dismiss the secondWindow they react? They have their own navs and aren’t on mainVC’s nav’s stack. Strange
– Lance Samaria
2 days ago




@zombie’s answer about the updating on the main thread 100% worked. On another note he also suggested I use symbolic breakpoints to diagnose the problem. He provided a great link to help:



Symbolic Breakpoints






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV