0

I am trying to apply the gradient color to UINavigationBar.appearence(). For this ,I followed the link. But after adding that extension of CAGradientLayer class,I have written the below lines of code in did finish launching with options in APPDelegate class. But I am getting an error like "Value of optional type UIImage? must be unwrapped to a value of type UIImage".Here am using UINavigationbar. Not the Navigationcontroller

Can anyone help me to do this, would be great.

// In AppDelegate Class

let navigationbar=UINavigationBar()
        let flareGradientImage = CAGradientLayer.primaryGradient(on: navigationbar)
        UINavigationBar.appearance().tintColor = UIColor(patternImage: flareGradientImage)



        UINavigationBar.appearance().tintColor = UIColor.white
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
Asad Ali Choudhry
  • 4,985
  • 4
  • 31
  • 36
anisha
  • 61
  • 9

1 Answers1

0

First your CAGradientLayer extension is getting bounds of navigation bar. Just initialising to UINavigationBar() won't work as its bound will be (0,0,0,0) and secondly you need to use below code to set it as backgroundImage.

UINavigationBar.appearance().setBackgroundImage(flareGradientImage, for:.default)
DivyaS
  • 141
  • 6