As per the documentation link, the Tab initializer in SwiftUI should allow supplying a custom view to the Label. However, the colors used within the Label view are not being honored as expected.
I attempted to set custom colors in the Label, but they either default to system-defined styles or are ignored entirely. This behavior does not align with my understanding of how custom views should work in SwiftUI's Label.
Am I missing a step or configuration here, or is this a bug in the current implementation?
struct ContentView: View {
@State private var activeTab: TabItem = .homeTab
var body: some View {
TabView(selection: $activeTab) {
ForEach(TabItem.allCases) { tabItem in
Tab(value: tabItem) {
getView(for: tabItem)
} label: {
VStack(spacing: 0) {
MainTabButtonView(
selected: activeTab == tabItem,
tabItem: tabItem
)
Text(tabItem.title)
}
}
}
}
}
}
private extension ContentView {
@ViewBuilder
func getView(for tabItem: TabItem) -> some View {
switch tabItem {
case .homeTab:
Text("Home")
case .searchTab:
Text("Search")
case .profileTab:
Text("Profile")
case .moreTab:
Text("More")
}
}
}
#Preview {
ContentView()
}
enum TabItem: String, Identifiable, Hashable, CaseIterable {
case homeTab
case searchTab
case profileTab
case moreTab
var tabImage: String {
switch self {
case .homeTab:
"house"
case .searchTab:
"magnifying-glass"
case .profileTab:
"biographic"
case .moreTab:
"hamburger-menu"
}
}
var title: String {
switch self {
case .homeTab:
"Home"
case .searchTab:
"Search"
case .profileTab:
"Profile"
case .moreTab:
"More"
}
}
var id: String {
rawValue
}
}
struct MainTabButtonView: View {
private let selected: Bool
private let tabItem: TabItem
init(
selected: Bool,
tabItem: TabItem
) {
self.selected = selected
self.tabItem = tabItem
}
var body: some View {
Image(tabItem.tabImage)
.renderingMode(.template)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 30, height: 30)
.foregroundStyle(
selected ? Color.green : Color.orange
)
}
}
Expected Behavior: The custom colors applied within the Label should render as defined.
Actual Behavior: The colors are overridden or ignored, defaulting to the system-defined styles.
Environment:
Xcode Version: Xcode 16.2 iOS: 18.2 Swift Version: Swift 6