4

When I write like this, it works as expected:

override func viewDidLoad() {
   super.viewDidLoad()
   MBProgressHUD.showAdded(to: navigationController!.view, animated: true)
}

However, when I put it in DispatchQueue.main block, the hud doesn't show:

override func viewDidLoad() {
   super.viewDidLoad()

   DispatchQueue.main.async {
      MBProgressHUD.showAdded(to: self.navigationController!.view, animated: true)
   }
}

I debug the view hierarchy, and there is a layout issue:

"Position and size are ambiguous for MBProgressHUD"

The navigationController is a childViewController of fatherViewController and the container view is set by auto layout in fatherViewController's view.

Is that cause the issue?

a_tuo
  • 651
  • 7
  • 23
  • you can write `self.view` instead of `self.navigationController!.view` or do you have your `UINavigationCotroller` added in storyboard if you have than make it as initialViewController connect as RootViewControlller than than Try adding `self.navigationController!.view` – Dhiru Jun 26 '17 at 05:55

4 Answers4

3

I have built a demo to test it, but it didn't occur. So I rechecked my code and found I put some work in DispatchQueue.main.async which block the UI and cause the problem.

a_tuo
  • 651
  • 7
  • 23
2

I performed test especially for you and following code works for me:

override func viewDidLoad() {
   super.viewDidLoad()

   DispatchQueue.main.async {
      MBProgressHUD.showAdded(to: self.navigationController!.view, animated: true)
   }
}

so the problem is somewhere else located

Robert
  • 3,790
  • 1
  • 27
  • 46
  • I have updated the question. The navigationController is a childViewController and it's container view is set by auto layout. I think maybe this cause the issue. – a_tuo Jun 21 '17 at 20:19
2

The root UIView geometry is not being calculated(viewDidLayoutSubviews is not called) at the point of viewDidLoad. I'd recommend putting your code into viewDidAppear. Also, no need to explicitly call it inside DispatchAsync.main, as other mentioned it is being called in the UI thread unless you want to call it from the background thread.

George Maisuradze
  • 1,953
  • 19
  • 16
0

Actually MBProgressHUD works on UI main thread and you are trying to call it in background thread.

Try this:-

 override func viewDidLoad() {
       super.viewDidLoad()
       DispatchQueue.main.async {  start()
 }
}
func start(){


  MBProgressHUD.showAdded(to: self.navigationController!.view, animated: true)}