iOS 26中无法修改未选中TabItem标题颜色问题求助
解决iOS 26中TabBar未选中项颜色无法设置的问题
问题原因
iOS 26对UITabBar的外观配置逻辑做了调整,原有的UITabBarItemAppearance配置可能被系统默认属性覆盖,导致未选中项的图标和标题颜色无法生效。
方案1:调整UIKit配置代码
在原有配置基础上,显式设置UITabBar的unselectedItemTintColor属性,确保未选中项颜色被正确应用:
import SwiftUI @main struct ExampleApp: App { init() { let appearance = UITabBarAppearance() appearance.configureWithOpaqueBackground() func configure(_ itemAppearance: UITabBarItemAppearance) { itemAppearance.normal.iconColor = .yellow itemAppearance.normal.titleTextAttributes = [ .foregroundColor: UIColor.yellow ] itemAppearance.selected.iconColor = .red itemAppearance.selected.titleTextAttributes = [ .foregroundColor: UIColor.red ] } configure(appearance.stackedLayoutAppearance) configure(appearance.inlineLayoutAppearance) configure(appearance.compactInlineLayoutAppearance) UITabBar.appearance().standardAppearance = appearance UITabBar.appearance().scrollEdgeAppearance = appearance // 显式设置未选中项的整体色调 UITabBar.appearance().unselectedItemTintColor = .yellow // 显式设置选中项色调 UITabBar.appearance().selectedItemTintColor = .red } var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { var body: some View { TabView { Color.red.opacity(0.1) .tabItem { Image(systemName: "house.fill") Text("Home") } Color.green.opacity(0.1) .tabItem { Image(systemName: "magnifyingglass") Text("Search") } Color.blue.opacity(0.1) .tabItem { Image(systemName: "heart.fill") Text("Favorites") } Color.purple.opacity(0.1) .tabItem { Image(systemName: "person.fill") Text("Profile") } } } }
方案2:使用SwiftUI原生修饰符(更简洁)
放弃UIKit的配置方式,直接用SwiftUI提供的unselectedTabItemTintColor和tint修饰符,适配iOS 15+:
import SwiftUI @main struct ExampleApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { var body: some View { TabView { Color.red.opacity(0.1) .tabItem { Image(systemName: "house.fill") Text("Home") } Color.green.opacity(0.1) .tabItem { Image(systemName: "magnifyingglass") Text("Search") } Color.blue.opacity(0.1) .tabItem { Image(systemName: "heart.fill") Text("Favorites") } Color.purple.opacity(0.1) .tabItem { Image(systemName: "person.fill") Text("Profile") } } .tint(.red) // 设置选中项的颜色 .unselectedTabItemTintColor(.yellow) // 设置未选中项的颜色 } }
说明
- 方案1兼容原有UIKit配置逻辑,适合需要更复杂TabBar外观定制的场景;
- 方案2完全使用SwiftUI原生API,代码更简洁,推荐在纯SwiftUI项目中使用。
内容的提问来源于stack exchange,提问作者Never_be
相关产品推荐
相关产品推荐

