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

SwiftUI 4中如何修改TabBarItem未激活图标的默认灰色?

修改SwiftUI 4未激活TabBarItem图标默认颜色(Xcode 14.2)

默认情况下,TabBar未激活项的图标为灰色,在SwiftUI 4(Xcode 14.2)环境中,可借助UIKit的UITabBarAppearance来修改该颜色,以下是两种常用实现方式:

方法一:全局设置(作用于App内所有TabBar)

在App的初始化方法中配置全局TabBar外观,所有TabView都会自动应用该样式:

@main
struct YourAppName: App {
    init() {
        // 创建TabBar外观配置对象
        let tabBarAppearance = UITabBarAppearance()
        
        // 设置未激活状态的图标颜色(替换为你需要的颜色,比如.systemGreen、.blue等)
        tabBarAppearance.stackedLayoutAppearance.normal.iconColor = .systemGreen
        // 可选:同步设置未激活状态的文字颜色
        tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor: UIColor.systemGreen
        ]
        
        // 将配置应用到全局TabBar
        UITabBar.appearance().standardAppearance = tabBarAppearance
        
        // iOS 15+适配:保证滚动到边缘时TabBar样式一致
        if #available(iOS 15.0, *) {
            UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
        }
    }
    
    var body: some Scene {
        WindowGroup {
            TabView {
                Text("首页")
                    .tabItem {
                        Image(systemName: "house")
                        Text("首页")
                    }
                Text("我的")
                    .tabItem {
                        Image(systemName: "person")
                        Text("我的")
                    }
            }
        }
    }
}

方法二:针对单个TabView设置

如果仅需修改某一个TabView的未激活颜色,可在该TabView的onAppear回调中配置:

struct ContentView: View {
    var body: some View {
        TabView {
            Text("首页")
                .tabItem {
                    Image(systemName: "house")
                    Text("首页")
                }
            Text("我的")
                .tabItem {
                    Image(systemName: "person")
                    Text("我的")
                }
        }
        .onAppear {
            let tabBarAppearance = UITabBarAppearance()
            // 设置未激活图标颜色
            tabBarAppearance.stackedLayoutAppearance.normal.iconColor = .purple
            // 可选:设置未激活文字颜色
            tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [
                NSAttributedString.Key.foregroundColor: UIColor.purple
            ]
            
            // 将配置应用到当前TabBar
            UITabBar.appearance().standardAppearance = tabBarAppearance
            
            if #available(iOS 15.0, *) {
                UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
            }
        }
    }
}

补充说明

  • stackedLayoutAppearance对应默认的堆叠式TabBar样式,若使用其他样式(如inline或compact),需修改对应的inlineLayoutAppearance或compactInlineLayoutAppearance。
  • 若需修改激活状态的图标/文字颜色,可设置stackedLayoutAppearance.selected.iconColor和stackedLayoutAppearance.selected.titleTextAttributes。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 00:31:14