12

The app is being crashed when trying to hide the navigation bar for a viewcontroller only for iOS 13 users.

  • I got the crash on Crashlytics of Fabric which titled as below,

Fatal Exception: NSInternalInconsistencyException

threading violation: expected the main thread

  • The app is being crashed on this line of viewWillAppear method,
    self.navigationController?.isNavigationBarHidden = true
  • To hide the navigation bar on the viewWillAppear method of UIViewController, I am using below code,
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = true
        super.viewWillAppear(animated)
    }
  • To show the navigation bar on the viewWillDisappear method of UIViewController, I am using below code,
    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = false
        super.viewWillDisappear(animated)
    }
Community
  • 1
  • 1
Nikunj
  • 630
  • 1
  • 6
  • 20
  • Are you doing this procedure in a thread different from the Main one? – Mattia Righetti Oct 11 '19 at 06:43
  • I think this way it the safe way to help you hide navigation https://stackoverflow.com/a/6418606/2454521 – Quang Dam Oct 11 '19 at 06:59
  • @MattiaRighetti, I am not doing this procedure in any thread. – Nikunj Oct 11 '19 at 07:32
  • @QuangDam Thanks for the solution, but Will this solution resolve the crash? – Nikunj Oct 11 '19 at 07:33
  • @Nikunj, you should scroll up the page I sent you, you will see a lot of answers about the problem. I think it can be useful – Quang Dam Oct 11 '19 at 07:45
  • Are you instantiating that viewController in a background thread? `threading violation: expected the main thread` is the key to your crash. – Starsky Oct 11 '19 at 08:19
  • @Starsky I am not instantiating that viewController in a background thread. – Nikunj Oct 11 '19 at 11:46
  • @QuangDam again I got crashes same way after applying the solution, but it is a few as compared to the previous crash, please help if you have any solution regarding 'expected the main thread' – Nikunj Oct 21 '19 at 04:21
  • https://stackoverflow.com/questions/58499124 @QuangDam please checkout this issue, which I have got after applying the solution – Nikunj Oct 23 '19 at 09:40

1 Answers1

13

do following action

  DispatchQueue.main.async {
    self.navigationController?.isNavigationBarHidden = false
  }
Saeed
  • 412
  • 5
  • 16