Ashutosh Mishra
Ashutosh Mishra

Reputation: 1939

Remove Gradient and make Transparent navigation bar in ios 15

I have tried the following code to remove the gradient from navigation and make a transparent navigation bar for ios 15, But it didn't work on the same whereas in the lower version it works perfectly fine.

if let bar =  self.navigationController?.navigationBar {
            if #available(iOS 15.0, *) {
                let navigationBarAppearance = UINavigationBarAppearance()
                navigationBarAppearance.configureWithTransparentBackground()
                navigationBarAppearance.shadowImage = UIImage()
                self.title = ""
                navigationBarAppearance.backgroundImage = UIImage()
                navigationBarAppearance.backgroundColor = .clear
                UINavigationBar.appearance().isTranslucent = true
                UINavigationBar.appearance().standardAppearance = navigationBarAppearance
                UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance   
        
            } else {
                bar.setBackgroundImage(FCUtil.getImageWithColor(color: UIColor.clear, size: bar.frame.size), for: UIBarMetrics.default)
                bar.isTranslucent = true
                bar.shadowImage = UIImage()
                self.title = ""
            }
        }

Please suggest to me, If I had missed something.

Upvotes: 1

Views: 407

Answers (1)

matt
matt

Reputation: 535138

The only problem with your code is the timing. You are running the code too late. Changing the navigation bar appearance() cannot change a navigation bar that already exists.

Instead, just change bar itself, directly:

bar.standardAppearance = navigationBarAppearance
bar.scrollEdgeAppearance = navigationBarAppearance   

Upvotes: 1

Related Questions