如何以编程方式配置UITabBarController而不传入视图控制器数组?
不依赖初始化时的视图控制器数组设置UITabBarController的方法
下面是几种可行的实现方案,根据你的需求选择:
方案1:直接配置UITabBar的Items,手动处理点击逻辑
可以初始化UITabBarController时不传入viewControllers数组,直接给tabBar.items赋值,然后通过代理处理标签切换事件:
// 初始化TabBarController,无需传入viewControllers let tabBarVC = UITabBarController() tabBarVC.delegate = self // 手动创建TabBarItem let homeItem = UITabBarItem(title: "首页", image: UIImage(systemName: "house"), selectedImage: UIImage(systemName: "house.fill")) let profileItem = UITabBarItem(title: "我的", image: UIImage(systemName: "person"), selectedImage: UIImage(systemName: "person.fill")) // 给系统TabBar设置items tabBarVC.tabBar.items = [homeItem, profileItem]
然后实现UITabBarControllerDelegate的方法来处理切换:
extension YourViewController: UITabBarControllerDelegate { func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController?) { // 这里根据选中的index处理视图切换逻辑 guard let selectedIndex = tabBarController.tabBar.selectedItem?.tag else { return } switch selectedIndex { case 0: // 切换到首页视图 break case 1: // 切换到我的视图 break default: break } } }
注意:这种方式下系统不会自动关联视图控制器,所有的视图切换逻辑需要自己实现。
方案2:自定义UITabBarController子类,动态配置视图控制器
通过自定义子类,在初始化后(比如viewDidLoad)再动态添加视图控制器,无需在初始化时传入数组:
class CustomTabBarVC: UITabBarController { override func viewDidLoad() { super.viewDidLoad() // 按需创建视图控制器并配置tabBarItem let homeVC = UIViewController() homeVC.tabBarItem = UITabBarItem(title: "首页", image: UIImage(systemName: "house"), selectedImage: UIImage(systemName: "house.fill")) let profileVC = UIViewController() profileVC.tabBarItem = UITabBarItem(title: "我的", image: UIImage(systemName: "person"), selectedImage: UIImage(systemName: "person.fill")) // 动态赋值viewControllers数组,保留系统原生切换逻辑 self.viewControllers = [homeVC, profileVC] } }
使用时直接初始化子类即可:
let tabBarVC = CustomTabBarVC()
方案3:隐藏系统TabBar,使用自定义底部栏
如果需要完全自定义标签栏的样式和交互,可以直接隐藏系统的TabBar,自己实现一个底部栏:
let tabBarVC = UITabBarController() // 隐藏系统TabBar tabBarVC.tabBar.isHidden = true // 创建自定义底部栏 let customTabBar = UIView() customTabBar.backgroundColor = .white customTabBar.frame = CGRect(x: 0, y: UIScreen.main.bounds.height - 83, width: UIScreen.main.bounds.width, height: 83) // 添加首页按钮 let homeBtn = UIButton(type: .system) homeBtn.setTitle("首页", for: .normal) homeBtn.setImage(UIImage(systemName: "house"), for: .normal) homeBtn.frame = CGRect(x: 20, y: 10, width: 60, height: 63) homeBtn.addTarget(self, action: #selector(switchToHome), for: .touchUpInside) customTabBar.addSubview(homeBtn) // 添加我的按钮 let profileBtn = UIButton(type: .system) profileBtn.setTitle("我的", for: .normal) profileBtn.setImage(UIImage(systemName: "person"), for: .normal) profileBtn.frame = CGRect(x: UIScreen.main.bounds.width - 80, y: 10, width: 60, height: 63) profileBtn.addTarget(self, action: #selector(switchToProfile), for: .touchUpInside) customTabBar.addSubview(profileBtn) // 将自定义栏添加到TabBarController的视图中 tabBarVC.view.addSubview(customTabBar)
然后实现对应的切换方法:
@objc func switchToHome() { // 切换到首页视图控制器 tabBarVC.viewControllers = [UIViewController()] } @objc func switchToProfile() { // 切换到我的视图控制器 tabBarVC.viewControllers = [UIViewController()] }
内容的提问来源于stack exchange,提问作者Muhamad
相关产品推荐
相关产品推荐

