4

Since iOS 16, we can customize the background of a navigationBar easily with:

.toolbarBackground(barBackgroundGradient, for: .navigationBar)
.toolbarBackground(.visible, for: .navigationBar)

It's working well.

However, I can't make it working for a bottom toolbar:

.toolbarBackground(barBackgroundGradient, for: .bottomBar)
.toolbarBackground(.visible, for: .bottomBar)

It's compiling but nothing changes on the app.

Looking at developer documentation, it should work...

I tried also that:

.toolbarBackground(barBackgroundGradient, for: .navigationBar, .bottomBar)

It doesn't change anything.

Any idea ?

alex.bour
  • 2,842
  • 9
  • 40
  • 66

1 Answers1

0

After some searching for an answer to a similar problem, my solution was to create an UIToolbar extension like this:

extension UIToolbar {
    static func changeAppearance(clear: Bool) {
        let appearance = UIToolbarAppearance()
        
        if clear {
            appearance.configureWithOpaqueBackground()
        } else {
            appearance.configureWithDefaultBackground()
        }

        // customize appearance for your needs here
        appearance.shadowColor = .clear
        appearance.backgroundColor = .clear
        appearance.backgroundImage = UIImage(named: "imageName")
        
        UIToolbar.appearance().standardAppearance = appearance
        UIToolbar.appearance().compactAppearance = appearance
        UIToolbar.appearance().scrollEdgeAppearance = appearance
    }
}

So, I think, in your case it's possible to create your barBackgroundGradient with CAGradientLayer first, then turn it to UIImage and use it in appearance.backgroundImage.

After all that call changeAppearance inside the initializer of your View:

init() {
    UIToolbar.changeAppearance(clear: true)
}

Probably, not the most elegant way, especially concerning the gradient part, so maybe you will find some other solution before Apple will fix this, as it seems to be a bug.