ios - 推送到 tabBar 时隐藏 View - swift

标签 ios swift uinavigationcontroller uitabbarcontroller tabbar

我有一个如下所示的 View Controller 。

enter image description here

这个 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 。

enter image description here

最佳答案

您需要设置从 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 array tabBarController?.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/

相关文章:

ios - 我可以从我的 UIViewController 中的 UINavigationController 继承吗?

ios - 在 navigationController 中呈现 viewController

ios - objective-c :对二维数组进行排序

ios - 调用 'becomeFirstResponder()' 的结果是 UITableView 中未使用的 UITextField

ios - UIPageViewController中的录音功能

swift - 如何在单击按钮时将纵向模式更改为横向模式?

ios - iCarousel 滚动不顺畅

iOS 持续任务完成

image - 使用 Swift 创建随机图像生成器

ios - iOS 编程 View 之间的交换/转换