Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SwiftUI custom tab bar and navigation bar

I was wondering why my custom toolbar effect works only for Navigation bar but not Tab bar. I made a simple clean new code to see if the effect works or not, and seems to not work. So my question is why does the effect only work for the Navigation bar? Using iOS 17. How can I achieve the effect for both bars?

enter image description here

import SwiftUI

@main
struct trash_testApp: App {
    
    init() {
        // Customize UINavigationBar
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.backgroundEffect = UIBlurEffect(style: .systemUltraThinMaterial)
        navBarAppearance.backgroundColor = UIColor.black.withAlphaComponent(0.7)
        navBarAppearance.shadowColor = .clear
        UINavigationBar.appearance().standardAppearance = navBarAppearance
        //UINavigationBar.appearance().compactAppearance = navBarAppearance
        UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearance

        // Customize UITabBar
        let tabBarAppearance = UITabBarAppearance()
        tabBarAppearance.backgroundEffect = UIBlurEffect(style: .systemUltraThinMaterial)
        tabBarAppearance.backgroundColor = UIColor.black.withAlphaComponent(0.7)
        tabBarAppearance.shadowColor = .clear
        UITabBar.appearance().standardAppearance = tabBarAppearance
        UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance
    }
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

ContentView:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            List(1...40, id: \.self) { index in
                Text("Row \(index): \(randomText())")
            }
            .navigationBarTitle("Top Toolbar", displayMode: .inline)
            .toolbar {
                // Top Toolbar Items
                ToolbarItem(placement: .navigationBarLeading) {
                    Button("Leading") {
                    }
                }
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("Trailing") {
                    }
                }
                // Bottom Toolbar Items
                ToolbarItem(placement: .bottomBar) {
                    HStack {
                        Button("Bottom Left") {
                        }
                        Spacer()
                        Button("Bottom Right") {
                        }
                    }
                }
            }
        }
    }

    func randomText() -> String {
        let words = ["SwiftUI", "List", "Example", "Random", "Text", "Row", "View"]
        let randomWord = words.randomElement() ?? "Word"
        return "This is a \(randomWord)"
    }
}

>Solution :

The bar added by ToolbarItem(placement: .bottomBar) is not a UITabBar (which is added by a TabView). It’s a UIToolbar. You should set UIToolbar.appearance() instead.

let toolbarBackground = UIToolbarAppearance()
toolbarBackground.backgroundEffect = UIBlurEffect(style: .systemUltraThinMaterial)
toolbarBackground.backgroundColor = UIColor.black.withAlphaComponent(0.7)
toolbarBackground.shadowColor = .clear
UIToolbar.appearance().standardAppearance = toolbarBackground
UIToolbar.appearance().scrollEdgeAppearance = toolbarBackground
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading