我有一个如下所示的 View Controller 。
这个 View 附加了一个 tabBarController。 tabBarController 有 5 个 viewController,我必须从另一个页面显示 tabBar 的第 5 个 viewController。所以我使用下面的代码来呈现 viewController
@IBAction func onClickUserProfile(_ sender: Any) {
let navVc = self.storyboard?.instantiateViewController(withIdentifier: "ProfileVC")as! ProfileVC
navVc.userId = Int(self.userId)
navVc.navigationItem.hidesBackButton = true
navVc.tabBarController?.tabBar.isHidden = false
self.navigationController?.pushViewController(nxtVc, animated: true)
}
但在执行代码后,它会生成如下图所示的 View Controller 。 View 经过 tabBar。任何人都可以帮助我推送到 tabBar View 。
最佳答案
您需要设置从 UITabBarController
中选择的 UIViewController
,像这样应该可以。
self.tabBarController?.selectedViewController = self.tabBarController?.viewControllers![1]
其中 tabBarController?.viewControllers
返回嵌入在 UITabBarController
中的当前 ViewControllers
的数组。
你的代码应该是这样的。
@IBAction func onClickUserProfile(_ sender: Any) {
let vc = self.tabBarController?.viewControllers![1] as! ProfileVC // use your index
vc.userId = Int(self.userId)
self.tabBarController?.selectedViewController = vc
}
Note: Don't create an instance of the
UIViewController
as.instantiateViewController(withIdentifier:)
use the already existed ones in the arraytabBarController?.viewControllers
, creating new instance will be treated as new one and gives the problem you have up there .
关于ios - 推送到 tabBar 时隐藏 View - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56574152/