You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS Swift中如何让UITabBarController默认取消所有标签选中状态?

iOS实现UITabBarController默认展示页面且所有标签未选中的可行方案

当然可以实现,核心思路是让UITabBarController默认加载指定页面,但通过自定义TabBar的外观,让所有标签项视觉上处于未选中状态,用户点击后再恢复正常的选中样式。以下是两种可靠的实现方式:

方法1:通过修改TabBarItem的选中样式实现

这种方式直接操作TabBarItem的图片和颜色属性,兼容性覆盖iOS 10+:

class CustomTabBarController: UITabBarController, UITabBarControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
        
        // 默认展示第一个页面(按需修改索引)
        selectedIndex = 0
        
        // 初始状态:让所有标签看起来未选中
        resetTabBarToUnselectedState()
    }
    
    private func resetTabBarToUnselectedState() {
        guard let items = tabBar.items else { return }
        let unselectedColor = tabBar.unselectedItemTintColor ?? .systemGray
        
        // 临时把选中颜色改成未选中颜色
        tabBar.tintColor = unselectedColor
        
        // 让所有TabBarItem的选中图片和默认图片一致
        for item in items {
            if let normalImage = item.image {
                item.selectedImage = normalImage.withRenderingMode(.alwaysOriginal)
            }
        }
    }
    
    // 用户点击标签时,恢复正常选中样式
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        guard let selectedItem = tabBar.selectedItem else { return }
        let selectedColor = UIColor.systemBlue // 自定义你的选中颜色
        
        // 先重置所有标签为未选中状态
        resetTabBarToUnselectedState()
        
        // 设置当前选中标签的样式
        tabBar.tintColor = selectedColor
        if let normalImage = selectedItem.image {
            selectedItem.selectedImage = normalImage.withTintColor(selectedColor, renderingMode: .alwaysOriginal)
        }
    }
}

方法2:使用UITabBarAppearance(iOS 13+)

iOS 13及以上推荐使用UITabBarAppearance来统一管理TabBar的外观,代码更简洁:

class CustomTabBarController: UITabBarController, UITabBarControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
        
        selectedIndex = 0
        setupInitialTabBarAppearance()
    }
    
    private func setupInitialTabBarAppearance() {
        let unselectedColor = tabBar.unselectedItemTintColor ?? .systemGray
        let appearance = UITabBarAppearance()
        
        // 让选中状态的样式和未选中一致
        appearance.stackedLayoutAppearance.selected.iconColor = unselectedColor
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor: unselectedColor
        ]
        
        tabBar.standardAppearance = appearance
        if #available(iOS 15.0, *) {
            tabBar.scrollEdgeAppearance = appearance
        }
    }
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        let selectedColor = UIColor.systemBlue
        let unselectedColor = tabBar.unselectedItemTintColor ?? .systemGray
        let appearance = UITabBarAppearance()
        
        // 设置选中状态样式
        appearance.stackedLayoutAppearance.selected.iconColor = selectedColor
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor: selectedColor
        ]
        
        // 保持未选中状态样式不变
        appearance.stackedLayoutAppearance.normal.iconColor = unselectedColor
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor: unselectedColor
        ]
        
        tabBar.standardAppearance = appearance
        if #available(iOS 15.0, *) {
            tabBar.scrollEdgeAppearance = appearance
        }
    }
}

关键注意事项

  1. 我们并没有修改UITabBarController的selectedIndex核心逻辑,只是改变了视觉表现,这样系统的页面切换逻辑依然正常工作。
  2. 必须设置UITabBarControllerDelegate,否则无法在用户点击时恢复选中样式。
  3. 如果使用自定义图片,要确保图片的renderingMode设置正确(比如.alwaysOriginal),避免颜色被系统默认 tint 覆盖。

内容的提问来源于stack exchange,提问作者Sachu_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 23:10:31