I want to add gradient color in UINavigationBar
. code for horizontal gradient working perfectly but for vertical gradient, its not showing proper color.
Thanx in advance!!!
My Current Code for vertical gradient :
extension UINavigationBar {
/// Applies a background gradient with the given colors
func applyNavigationGradient( colors : [UIColor]) {
let gradientLayer = CAGradientLayer()
gradientLayer.frame = self.bounds
gradientLayer.colors = colors.map { $0.cgColor }
// *** for horizontal gradient ***
// gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5)
// gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5)
// *** for vertical gradient ***
// gradientLayer.locations = [0,1]
gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.0)
gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0)
UIGraphicsBeginImageContext(gradientLayer.bounds.size)
gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
setBackgroundImage(image, for: UIBarMetrics.default)
}
}