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?
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:
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
