How to change the colour of back button? - ios

I have a map with just a back button at the top of the screen. For some reason it wont change colour for me. This is the code that I have in the delegate to change the other back button in the delegate.
UIBarButtonItem *barButtonAppearance = [UIBarButtonItem appearance];
[barButtonAppearance setTintColor:[UIColor blackColor]];
Cheers!

To change the text color of a single button you might set an attributed string as its title:
NSDictionary *attributes = #{NSForegroundColorAttributeName: [UIColor redColor]};
[button setTitleTextAttributes:attributes forState:UIControlStateNormal];
To change the color of all buttons in your navigation bar try:
self.navigationController.navigationBar.tintColor = [UIColor redColor];
The last one will also change the tint color if you have an image instead of text.
Both methods are tested unter iOS7.

Related

UINavigationBar: Adding navigation bar not able to change color or title

I add it a UINavigationBar on interface builder on my story board but I want to change the color and title of the UINavigationBar programmatically but it doesn't work. Here is my code:
self.navigationController.navigationBar.tintColor = [UIColor redColor];
self.navigationItem.title = #"New Title";
Any of you knows why this happening or a way around this?
I'll really appreciate your help.
Try:
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
These kinds of things can also be done in interface builder when you have access to the reference (and previewed)
try this code
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName:[UIColor blueColor]};
if you want to change navigation bar background color,
use
self.navigationController.navigationBar.barTintColor
you can also use these code to change the navigationbar in global.
[[UINavigationBar appearance] setBarTintColor:RGB(248, 248, 248)];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName:COLOR_NAV_GLOBAL}];
[[UINavigationBar appearance] setBackIndicatorImage:[[UIImage imageNamed:#"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[[UIImage imageNamed:#"nav_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:33.0/255.0 green:30.0/255.0 blue:94.0/255.0 alpha:1.0];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
put this in the view did load method.
first one is for the navigation bar tint color and the second one is for the title font color.

How to change text color for tab bar button in storyboard ios8

I changed tab bar background in Bar Tint field and want to change text colour for tab bar button. By default is grey and blue.
I try to change field Tint in section View, but it doesn't help.
NSDictionary *attribute = #{NSForegroundColorAttributeName:[UIColor redColor]};
[[UITabBarItem appearance] setTitleTextAttributes:attribute forState:UIControlStateNormal];
This will change the text colour for the tab bar button.
This is for selected state:
[[UITabBarItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor redColor]} forState:UIControlStateSelected];
This is for not selected state:
[[UITabBarItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor greenColor]} forState:UIControlStateNormal];
Try it
To change tabbar text color you can use this
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary [UIColor redColor], UITextAttributeTextColor,[UIColor redColor],UITextAttributeTextShadowColor,[NSValue valueWithUIOffset:UIOffsetMake(0, 0.5)], UITextAttributeTextShadowOffset,[UIFont fontWithName:#"Arial" size:10], UITextAttributeFont,nil]forState:UIControlStateNormal];

Setting Blue Text Color of BarButtonItem in UINavigation bar fails in iOS 7

I'm trying to change the Navbar tint at runtime, and I'm able to change the navigation bar tint, but then the left and right buttons (which were system blue on iOS 7) changed, so I'm trying to force them back to the system blue color. this is the code I am using, called from viewDidLoad: I've tried both NSForegroundColorAttributeName and UITextAttributeTextColor, but I keep getting white or black text, depending on the color of the navigation bar.
How can I get the buttons to show blue text?
NSDictionary *btnAttr = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:14], UITextAttributeFont,
[UIColor blueColor], NSForegroundColorAttributeName,
nil];
for(UIView *v in [theView.view allSubViews])
{
if([v isKindOfClass:[UINavigationItem class]])
{
UINavigationItem *btn = (UINavigationItem *)v;
[btn.leftBarButtonItem setTitleTextAttributes:btnAttr forState:UIControlStateNormal];
[btn.rightBarButtonItem setTitleTextAttributes:btnAttr forState:UIControlStateNormal];
}
}
In iOS7 you if you need to change the navigationBar buttons color, you need to set tintColor:
navigationController.navigationBar.tintColor = [UIColor blueColor];
For better understanding check the image below:

Customize the NavigationBar backBarButton

I use the code below to customize the backBarButtonItem, because I need to change the text color.
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
The color changed but the text font looks weird. See, the "Back" is not clear around the edges.
I don't know why this happens. Does anyone has any idea about this?
The text has a shadow, which is set upward from the text. textlabel's have a shadowOffset value and a shadowColor value. You can either offset the shadow differently to make it look better or change the color to [UIColor clearColor] - both of which could help.
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setValue:[UIColor blackColor] forKey:UITextAttributeTextColor];
[attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
Replacing your code with the above snippet will set the text color to black, and the shadow to clear. This should remove the bluriness. The blur is actually caused by a shadow being cast by the text. By making the shadow a 'clearColor' it becomes invisible. An alternative would be changing the offset so that it looks like it's being cast downwards instead of up; or change the color to something distinctly different from black so that you can distinguish between the text and it's shadow.

Title text color in bar button item

I have a bar button item that only functions to display a string that is updated when something is performed on screen. I have set the text color to white. But, it is displayed on screen with a gray color. Whatever color I change the text to I still get a grayish color instead of the desired color. Why am I not getting the the right color? Is it a property that I'm missing?
UIColor *buttonColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
_timeButton.title = [NSString stringWithFormat:#"Updated at: %#",dateString];
[_timeButton setStyle:UIBarButtonItemStylePlain];
_timeButton.tintColor = [UIColor clearColor];
[_timeButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:15], UITextAttributeFont,
buttonColor ,UITextAttributeTextColor,
nil]
forState:UIControlStateNormal];
Turns out that my text color of my button was gray because the button was not enabled. Setting the button to enabled changed the color of the text to white.
[_timeButton setEnabled:YES];

Resources