16.7 C
Canberra
Tuesday, April 28, 2026

ios – How can I alter the background colour of the chosen tab in SwiftUI TabView?


AFAIK, swiftUI TabView doesn’t present a lot management such as you can not change background colour as there is no such thing as a direct public api. Because it internally use UIKIt Tabbar which itself permits to alter icon and textual content colour solely.

I might advocate you to create your personal customTabView which may be very easy to create for e.g. under and this additionally animate whereas you choose the tabs

enter image description here

struct CustomTabView: View {

    @State personal var selectedTab: Tab = .as we speak
    @Namespace personal var namespace
    var physique: some View {
        VStack {
            Spacer()

            // Overlay Tab Bar
            HStack {
                tabItem(icon: "sparkles", title: "Chat", tab: .chat)
                tabItem(icon: "calendar", title: "At present", tab: .as we speak)
                tabItem(icon: "checklist.bullet", title: "Critiques", tab: .opinions)
                tabItem(icon: "sq..stack", title: "Area", tab: .area)
            }
            .padding(8)
            .background(
                RoundedRectangle(cornerRadius: 30)
                    .fill(Coloration(.systemGray6))
            )
            .padding(.horizontal, 16)
            .padding(.backside, 10)
        }
    }

    @ViewBuilder
    personal func tabItem(icon: String, title: String, tab: Tab) -> some View {
        Button {
            withAnimation(.easeInOut(length: 0.25)) {
                selectedTab = tab
            }
        } label: {
            VStack(spacing: 4) {
                Picture(systemName: icon)
                    .font(.system(dimension: 18, weight: .medium))

                Textual content(title)
                    .font(.system(dimension: 12))
            }
            .foregroundColor(selectedTab == tab ? .black : .grey)
            .body(maxWidth: .infinity)
            .padding(.vertical, 10)
            .background(
                ZStack {
                    if selectedTab == tab {
                        RoundedRectangle(cornerRadius: 20)
                            .fill(Coloration.inexperienced.opacity(0.3)) // your spotlight colour
                            .matchedGeometryEffect(id: "customBg", in: namespace)
                    }
                }
            )
        }
    }
}

You may replace your required colour accordingly!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles