I'd like to change programmaticaly the tintColor
of a UINavigationBar and keep the gradient as in Interface Builder.
When I change the tintColor
in my code, the gradient disappears but when I change the tintColor
in Interface Builder, the gradient is kept.
Any ideas?
I just wrote it as UIImage extension for Swift 2.0. Maybe it's of some use.
Call it like this:
Set the barStyle to UIBarStyleBlackTranslucent Set the tintColor like this:
This will set the appropriate gradient. Use whatever combination you need.
So this is an old question, but I stumbled upon it when I was searching for something that sounds like this question, but is different. In hopes that it will help someone else (or that someone else will show me a better way)...
How do you implement a custom gradient on a UINavigationBar?
First, let's get our custom gradient the way we normally would (As a CALayer):
There we go, a gradient that looks like a questionable make-up choice of a punk rocker, which is good, as this code is for the hypothetical app I Have A Punk-Rocker With Questionable Tastes In My Pocket. That name me be a little too long..
Now I want this layer on my UINavigationBar, which should be easy ya? Just add it as a sublayer, right? The problem here is it will cover up the wonderful buttons and stuff that the UINavigationController gives you. That's bad.
Instead let's look at the wonderful convenience method we have in iOS5+ for altering the appearance of all the UINavigationBar(s) in our app:
The only problem here is we don't have an UIImage, we have a CALayer. What's a punk-rocker enthusiast to do?
So now we've converted our CALayer to an UIImage (hopefully), so all that remains is to set it:
What I like about this solution is
But I'm still not convinced it's the best way. So, enjoy if it helps you, let me know how to improve it if you can.
~ Thanks
Good way use maskView