Search code examples
iosswiftuitabbarcontroller

Changing the selected index of tabbar controller displays a black screen


I am trying to navigate from one view controller to another using this code:

self.tabBarController?.selectedIndex = 1
self.tabBarController?.selectedViewController = self.tabBarController?.viewControllers![1]

When the code executes it goes to the desired UIViewController but the view appears after a small delay and a black screen is visible during that time.


Solution

  • Found the answer just had to run the code in main thread

    DispatchQueue.main.async {
      self.tabBarController?.selectedIndex = 1
      self.tabBarController?.selectedViewController = self.tabBarController?.viewControllers![1]
    }
    

    }