I want to set the UINavigationbar
backgroundColor
to a gradient color where I would like to set it via an array of colors to create a Gradient, ideally, as accessible methods inside UINavigationBar
to change its color to this gradient.
Any suggestions? (Aside from setting an image manually as the background image of the navigation bar)
This is the solution without using an intermediate
CAGradientLayer
, and just usingCoreGraphics
, in Swift 3.0.Essentially, the method creates a
UIImage
on the fly with the gradient colors passed and sets it.The
defer
statement makes this so much clean than prior versions. Be noted thatCGGradient
are available since iOS 8.0.Also, this creates the gradient from left to right, tweaking the parameters of
drawLinearGradient
(start
andend
) moves the locations. This is up for your implementation.