在 UITabBarController 中,您可以通过以下步骤将 UINavigationController 推入到 UITabBarController 中:

  1. 创建 UINavigationController 对象,并将需要推入的 UIViewController 对象设置为 UINavigationController 的根视图控制器。
let navigationController = UINavigationController(rootViewController: viewController)
  1. 将 UINavigationController 对象添加到 UITabBarController 的 viewControllers 数组中
tabBarController.viewControllers?.append(navigationController)
  1. 可选步骤: 设置 UINavigationController 的标题和图标。
navigationController.tabBarItem.title = 'Title'
navigationController.tabBarItem.image = UIImage(named: 'ImageName')

完整的代码示例:

let viewController = UIViewController()
viewController.view.backgroundColor = UIColor.white

let navigationController = UINavigationController(rootViewController: viewController)
navigationController.tabBarItem.title = 'Title'
navigationController.tabBarItem.image = UIImage(named: 'ImageName')

tabBarController.viewControllers?.append(navigationController)

注意: 需要将上述代码放置在 UITabBarController 的视图加载完毕后执行,例如在 viewDidLoad 方法中执行。

在UITabBarController中添加UINavigationController - 详细步骤

原文地址: https://www.cveoy.top/t/topic/o4hp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录