swift - 如何在 UITabBarViewController 的选项卡中显示?
问题描述
我希望能够创建一个带有一些选项卡的 TabBarViewController,然后推入给定的选项卡
let tabBarViewController = UITabBarController()
let redVc = UIViewController()
redVc.view.backgroundColor = .red
let blueVc = UIViewController()
blueVc.view.backgroundColor = .blue
tabBarViewController.viewControllers = [redVc, blueVc]
这创建了一个带有红色和蓝色选项卡的 tabBarViewController。现在我想把一个黄色的 VC 推到红色标签上,这样我就有了一个黄色和一个蓝色的标签。
let yellowVc = UIViewController()
yellowVc.view.backgroundColor = .yellow
tabBarViewController.modalPresentationStyle = .fullScreen
// this doesn't work
viewController.present(tabBarViewController, animated: true)
// must use this
tabBarViewController.viewControllers![0] = yellowVc
我应该怎么做才能在给定的选项卡中呈现?
解决方案
您可以设置您的 UITabbarController,它的每个孩子都是 UINavigationController。
总之,您将拥有以下层次结构:
UITabbarController
- 孩子1 (
UINavigationController
)- 第一个内容 ViewController
- 孩子2 (
UINavigationController
)- 第二个内容ViewController
- 孩子1 (
现在,在每个 contentViewController 中,您可以使用navigationcontroller.push
将新的 viewController 推送到堆栈中,它将保留在选项卡栏内。
推荐阅读
- javascript - 如何从 javascript 函数调用 vuejs 函数?
- javascript - node.js 无法从 ionic 4 发出 post 请求
- html - VBA。从站点解析数据
- ruby - ruby MS windows gem:win32/daemon:Service_Main线程异常退出
- nginx - Nginx 的 sub_filter 不会替换 CSS 和 JS 中的路径
- android - 棒棒糖设备上未安装 INSTALL_FAILED_SHARED_USER_INCOMPATIBLE 应用程序
- c# - ASP Net Core / Entity Framework 表单保存错误
- regex - 正则表达式完全匹配 - 数据表
- mongodb - 根据文档中存储的参数查找匹配的文档
- symfony - Symfony / Twig 数据库查询过多