更改标签栏控制器的选定索引显示黑屏

Changing the selected index of tabbar controller displays a black screen

我正在尝试使用以下代码从一个视图控制器导航到另一个视图控制器:

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

当代码执行时,它会转到所需的 UIViewController 但视图会在一小段延迟后出现,并且在此期间可以看到黑屏。

找到答案只需要 运行 主线程中的代码

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

}