我正在尝试将渐变颜色应用于UINavigationBar.appearence()
。为此,我遵循了link 。但是在添加 CAGradientLayer
类的扩展之后,我在 did finish launch with options in APPDelegate
类中编写了以下代码行。但我收到类似“可选类型 UIImage?
的值必须解包为 UIImage
类型的值”之类的错误。这里使用 UINavigationbar
。不是 Navigationcontroller
任何人都可以帮助我做到这一点,那就太好了。
// 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]
最佳答案
首先,您的 CAGradientLayer 扩展正在获取导航栏的边界。仅初始化 UINavigationBar() 是行不通的,因为它的边界将为 (0,0,0,0),其次您需要使用下面的代码将其设置为背景图像。
UINavigationBar.appearance().setBackgroundImage(flareGradientImage, for:.default)
关于ios - 导航栏的渐变颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56766552/