Changing the Tint Color of UIBarButtonItem - ios

I have a project using Storyboards and whenever I push a view controller with a segue, the dynamically created bar button item is always blue.
It's driving me nuts. Because this object is created dynamically, I cannot set its color in IB (like I have done with previous bar button items).
Among the solutions I have tried are:
Set it in the receiver's viewDidLoad
Set it in the receiver's viewDidAppear
self.navigationItem.backBarButtonItem.tintColor = [UIColor whiteColor];
When I saw that didn't quite work, I tried setting the leftBarButtonItem instead:
self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
I have tried the following code (which I got from other SO answers) in my app's delegate, when the new view gets called, and before pushing the new view:
[[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];
All the google answers I have found recommend to use the code above, but it's not working at all for me. Maybe there are some changes in iOS 7's appearance API? No matter how or where I try to set "Categorías" to white, it's always the default blue.

In iOS 7, to set the color of all barButtonItems in your app, set the tintColor property on the application's window in the AppDelegate.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.tintColor = [UIColor whiteColor];
return YES;
}
More detailed info in Apple's iOS 7 UI Transition Guide (Specifically under the 'Using Tint Color` section).
***OR***
Based on some of the comments, you can also achieve this with the UINavigationBar appearance proxy. This will affect the tintColor of only UIBarButtonItems, as opposed to setting the tintColor on the window and affecting all subviews of that window.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if([UINavigationBar conformsToProtocol:#protocol(UIAppearanceContainer)]) {
[UINavigationBar appearance].tintColor = [UIColor whiteColor];
}
return YES;
}

I think you are looking for a property of your UINavigationBar. Try setting self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
See "Appearance of Navigation Bars" section: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UINavigationBar.html#//apple_ref/doc/uid/TP40012857-UINavigationBar-SW1

In Swift 3.0
let navigationBarAppearnce = UINavigationBar.appearance()
A navigation bar’s tintColor affects the color of the back indicator image, button titles, and button images.
navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)
The barTintColor property affects the color of the bar itself
navigationBarAppearnce.tintColor = UIColor.white
Final Code
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let navigationBarAppearnce = UINavigationBar.appearance()
navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)
navigationBarAppearnce.tintColor = UIColor.white
navigationBarAppearnce.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
//Change status bar color
UIApplication.shared.statusBarStyle = .lightContent
return true
}

Swift 5
barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)

To change the color of a specific item (e.g. a button) in your nav bar:
In Objective-C
myButton.tintColor = [UIColor redColor];

In iOS 8 if you changed UIView tint color for some purpose, for example for branding UIAlertView, tint color for UIBarButtonItem in UIToolBar also changed that way. To fix this, just write this code
[UIView appearance].tintColor = SOME_COLOR;
[UIView appearanceWhenContainedIn:[UIToolbar class], nil].tintColor = BLACK_COLOR;
For UIBarButtonItem tint color in UINavigationBar use standard method
[UINavigationBar appearance].tintColor = BLACK_COLOR;

UITabBar.appearance().tintColor = UIColor.yellowColor()

This worked for me
AddBarButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)

Related

How to set exact color to uinavigationbar as given, i need below

All i want that the UINavigationBar and the searchView(CustomView)
should have exact same color and without having shadow under the UINavigataionbar. i have tried a lot,i found the soultion for shadow
UINavigationBar.appearance().shadowImage = UIImage()
but still can't able to do same background color on UINavigationBar and searchView. You can see that minor color difference between UINavigationBar and SearchView
Please don't go for guidelines, if u have answer please share.
Note: SearcView is UIView wid label and an image not UISeachBar.
Try setting translucent property of navigationBar to false
Swift
self.navigationController.navigationBar.isTranslucent = false
Objective C
[self.navigationController.navigationBar setTranslucent:NO];
PS: if you want to do it in all the screen, set it globally
Swift
UINavigationBar.appearance().isTranslucent = false
Objective C
[[UINavigationBar appearance] setTranslucent:NO];
Set your UINavigationBar translucent property false.
Storyboard: In Storyboard uncheck translucent property
Swift:
self.navigationController.navigationBar.isTranslucent = false
In AppDelegate (didFinishLaunchingWithOptions):
UINavigationBar.appearance().isTranslucent = false
You can change the color of navigation bar using this
UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor greenColor]]; //Change the color according to your requirement.
Try This One
Objective C
self.navigationController.navigationBarHidden=NO;
[self.navigationController.navigationBar setTitleTextAttributes: #{NSForegroundColorAttributeName:White,NSFontAttributeName:yourFont}];
self.navigationController.navigationBar.barTintColor = yourColor;
Swift
self.navigationController?.navigationBar.barTintColor = .red //yourColor
self.navigationController?.navigationBar.tintColor = .white //yourColor

How to make UINavigationBar background transparent?

First of all,
I've seen all the answers at How to make UINavigationBar Transparent in IOS 8? Transparent UINavigationBar and Make UINavigationBar transparent.
They just don't seem to work for me.
My regular view controller (before trying to make the navigation bar transparent) doesn't have any issues:
I'm using (tried both in viewDidLoad and viewWillAppear:):
[self.navigationController.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
I'm getting this:
Gray status bar background, completely white navigation bar which doesn't blend with the status bar, and then the view starts. All the 'solutions' at the other questions' answers' yield the same result for me.
I've also tried setting self.edgesForExtendedLayout = UIRectEdgeNone; or self.edgesForExtendedLayout = UIRectEdgeAll; but that also didn't have any impact.
How can I make my navigation bar transparent without messing up everything?
UPDATE: Following Warif Akhand Rishi's answer, I've changed self.navigationController.view.backgroundColor = [UIColor clearColor]; to self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];, now I'm getting a gray, unified status/navbar, but still not transparent:
UPDATE 2: I've hooked up the view debugger, and that gray background seems to come from deep down from the roots of view hierarchy, and my view's content is not extending up. I've tried self.edgesForExtendedLayout = UIRectEdgeAll; again with the latest code but still no avail:
swift 4 transparent nav bar:
(be sure view extends behind nav bar to show through, otherwise will just be black)
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage() //remove pesky 1 pixel line
or just match navbar color to color of your current vc, but keep it opaque. with translucent set to false child views will line up with navbar instead of going under it.
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.barTintColor = UIColor.yourColor
navigationController?.navigationBar.shadowImage = UIImage() //remove pesky 1 pixel line
Change your
self.navigationController.view.backgroundColor = [UIColor clearColor];
to this
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
Okay, after struggling, I've solved the problem on my own. There was more than one problem. It wasn't about the extended edges, it was about the line self.navigationController.view.backgroundColor = [UIColor clearColor]; (which had to be self.navigationController.navigationBar.backgroundColor = [UIColor clearColor]; as Warif Akhand Rishi suggested) and also my table view's clip subviews property. I've changed that line and also turned off clipping of my table view and now it works as expected.
For iOS 13 and the UINavigationBarAppearance API:
let navAppearance = UINavigationBarAppearance()
navAppearance.configureWithTransparentBackground()
self.navigationItem.standardAppearance = navAppearance
Eliminate 5+ lines of shadow/background/color code!
I'm a little late to the party, but I recently needed to do the same thing and I found the following actually works best (because it removes all shadows and bleed-throughs you may have from something lower in the stack):
guard let navBar = navigationController?.navigationBar else { return }
navBar.barStyle = .black
navBar.setBackgroundImage(UIImage(), for: .default)
navBar.shadowImage = UIImage()
navBar.isTranslucent = true
navBar.isHidden = false
1.Your NavigationBar is white,not black.So you must have a view (a white view) under NavigationBar,which is the superview of your greyView.The transparent setting works,but you cann't see it ,because the fontcolor is white too.
2.So you have to update your greyView's constraints,so it can extends under navigationbar .And then you can see your white title.
3.Maybe you have to Change your statusBar's UIStatusBarStyle to default or lightcontent,I noticed the font color of statusBar is white too.
The below code works for me
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
self.navigationController?.navigationBar.backgroundColor = .clear

Custom color and alpha in tab bar

i'm trying to change the color of a tabbar in my app, i use this line of code to change the color of it:
[[UITabBar appearance] setBarTintColor:[UIColor greenColor]];
I want to add a translucent iOS 7 effect but with a green color. I've changed translucent property but i don't see any result.
Change UITabBarController's alpha:
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController *tabBarController = (UITabBarController *) self.window.rootViewController;
[tabBarController.tabBar setBarTintColor:[UIColor greenColor]];
[tabBarController.tabBar setAlpha:0.2];
}
or with appearance in the same application: didFinishLaunchingWithOptions:
[[UITabBar appearance] setBarTintColor:[UIColor greenColor]];
[[UITabBar appearance] setAlpha:0.2];
You can either set the color on the storyboard by selecting the root: Tab Bar View Controller, select the tab bar, and adjust the Background (or tint) color in the attributes inspector, or you can adjust the code with the barTintColor:
// Adjust the Color of the Tab Bar itself
self.tabBar.barTintColor = [UIColor redColor];
// Adjust the Color of the selected Icon in the Tab Bar
self.tabBar.tintColor = [Single single].singleThemeColorTint;
If you need to adjust the ALPHA too, I would use:
UIColor *charcoal = [UIColor colorWithRed:66/255.0
green:79/255.0
blue:91/255.0
alpha:1];
// For Tab Bar
self.tabBar.barTintColor = charcoal;
// For selected Item Highlight
self.tabBar.tintColor = charcoal;
I created a View Controller File for the Tab Bar Story Board, and ran this code in ViewDidLoad{ }

How to change the background color of NavigationBar in iOS 7

I'm new to Objective-C, today I tried to change color of my Navigation Bar and this works with this code:
appDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0xf4f4f4)];
return YES;
}
Now I try change specific ViewController navigation bar and this do not work.
ViewController2.m:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UINavigationBar appearance] setBarTintColor:UIColorFromRGB(0x363636)];
self.navigationController.navigationBar.translucent = NO;
}
When I enter this ViewContoller his Navigation color is f4f4f4 and when I go other ViewController and come back it changes the color to 363636.
Why this do not work in first time?
Can somebody explain this to me..
(Sorry About my English, and thank you.)
when you use
[UINavigationBar appearance]
it changes all of the navbars. in your viewcontroller, just do
[self.navigationBar setBarTintColor:UIColorFromRGB(0x363636)];
You can implement what Nathanael said in the -(void) viewDidLoad method of the view controller. But if you're using the storyboard, you can also select the navigation item and change the tint color from the File inspector without having to code.
Use tintcolor property of navigation bar to change the color
you can try it, I change in my AppDelegate the navigation bar color, so all the app has the same color, but i have to change the navigation bar color in a single view controller so I did it:
in the AppDelegate
let color = UIColor(red:0.24, green:0.72, blue:0.28, alpha:1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barTintColor = color
and in the view controller in the viewDidLoad I have this:
self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()
in the viewWillDissappear this:
super.viewWillDisappear(animated)
let color = UIColor(red:0.24, green:0.72, blue:0.28, alpha:1.0)
self.navigationController?.navigationBar.barTintColor = color
It returns the navigation bar color to the same color
For iOS 10 you can use barStyle like this:
For white bar color:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
And for black bar color:
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
BarStyle need set in ViewDidLoad for normal animation work :)

How to change UINavigationBar background color from the AppDelegate

I know how to change the UINavigationBar background image by doing
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"nabbar"] forBarMetrics:UIBarMetricsDefault];
and I know how to set the bar to different colors within each Views..... Now I want to change the background color without using an image to a solid color from the app delegate. I do not want to set it each time from each view and I do not want to write a CGRect.
I tried [[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:33/255.0 green:34/255.0 blue:36/255.0 alpha:1.0]]; but I doesn't work and I cant find a code anywhere that works in the app delegate.
Could anyone please point me in the right direction?
You can use [[UINavigationBar appearance] setTintColor:myColor];
Since iOS 7 you need to set [[UINavigationBar appearance] setBarTintColor:myColor]; and also [[UINavigationBar appearance] setTranslucent:NO].
[[UINavigationBar appearance] setBarTintColor:myColor];
[[UINavigationBar appearance] setTranslucent:NO];
To change the background color and not the tint the following piece of code will work:
[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];
[self.navigationController.navigationBar setTranslucent:NO];
For doing this in iOS 7:
[[UINavigationBar appearance] setBarTintColor:myColor];
Swift syntax:
UINavigationBar.appearance().barTintColor = UIColor.whiteColor() //changes the Bar Tint Color
I just put that in the AppDelegate didFinishLaunchingWithOptions and it persists throughout the app
iOS 13.0 introduced new API for this:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let myColor = UIColor(hue: 0.4, saturation: 0.25, brightness: 1, alpha: 1)
let barAppearance = UINavigationBarAppearance()
barAppearance.backgroundColor = myColor
let navigationBar = UINavigationBar.appearance()
navigationBar.standardAppearance = barAppearance
navigationBar.scrollEdgeAppearance = barAppearance // for scrollable content or large titles
return true
}
UINavigationBarAppearance
Swift:
self.navigationController?.navigationBar.barTintColor = UIColor.red
self.navigationController?.navigationBar.isTranslucent = false
You can easily do this with Xcode 6.3.1. Select your NavigationBar in the Document outline. Select the Attributes Inspector. Uncheck Translucent. Set Bar Tint to your desired color. Done!
As the other answers mention, you can use setTintColor:, but you want a solid color and that's not possible to do setting the tint color AFAIK.
The solution is to create an image programmatically and set that image as the background image for all navigation bars via UIAppearance. About the size of the image, I'm not sure if a 1x1 pixel image would work or if you need the exact size of the navigation bar.Check the second answer of this question to see how to create the image.
As an advice, I don't like to "overload" the app delegate with these type of things. What I tend to do is to create a class named AppearanceConfiguration with only one public method configureAppearance where I set all the UIAppearance stuff I want, and then I call that method from the app delegate.
In Swift 4.2 and Xcode 10.1
You can change your navigation bar colour from your AppDelegate directly to your entire project.
In didFinishLaunchingWithOptions launchOptions: write below to lines of code
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor(red: 2/255, green: 96/255, blue: 130/255, alpha: 1.0)
Here
tintColor is for to set background images like back button & menu lines images etc. (See below left and right menu image)
barTintColor is for navigation bar background colour
If you want to set specific view controller navigation bar colour, write below code in viewDidLoad()
//Add navigation bar colour
navigationController?.navigationBar.barTintColor = UIColor(red: 2/255, green: 96/255, blue: 130/255, alpha: 1.0)
navigationController?.navigationBar.tintColor = UIColor.white
You can set UINavigation Background color by using this code in any view controller
self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:10.0f/255.0f green:30.0f/255.0f blue:200.0f/255.0f alpha:1.0f];
The colour code is the issue here. Instead of using 195/255, use 0.7647 or 195.f/255.f The problem is converting the float is not working properly. Try using exact float value.
Dealing with UINavigationController and UINavigationBar in iOS is a hassle. Fortunately, having an open source third-party library can easily solve these problems, hoping to help everyone.
Git repo: NXNavigationExtension
Change UINavigationBar color:
extension YourViewController {
override var nx_titleTextAttributes: [NSAttributedString.Key : Any]? {
return [NSAttributedString.Key.foregroundColor: .red]
}
}
📝 example

Resources