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

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:

Related

Not able to change navigationbar text color or tint color in Objective C

I want to set both navigation bar backgound color and navigation tint color so that I can set tint color of all system buttons present in the navigation bar. I have written the following code:
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
// iOS 7.0 or later
self.navigationController.navigationBar.barTintColor = [UIColor orangeColor];
self.navigationController.navigationBar.translucent = NO;
}else {
// iOS 6.1 or earlier
self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
}
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
This code only changes the background color of the navigation bar but not the tint color of buttons. Buttons are showing in default blue color. But while navigating to other screens some times buttons color gets changed to the color I set by the code above but this does not happens always.
This will surely work , call it in Appdelegate
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor whiteColor]];

How to Change Segment Separator Color?

In my Project i am using segment Controller .. they have four segment in my segment Controller ..My question is I want this background color and font color and State selected color and separator color[White when select the segment]
Like this Image
.
But My screen is
My code Is
- (void)viewDidLoad {
[self changeColor];
}
- (void)changeColor{
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithRed:83.0f/255.0f green:198.0f/255.0f blue:255.0f/255.0f alpha:1.0]} forState:UIControlStateSelected];
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithRed:197.0f/255.0f green:197.0f/255.0f blue:197.0f/255.0f alpha:1.0]} forState:UIControlStateNormal];
[mailboxsegment setTintColor:[UIColor colorWithRed:202.0f/255.0f green:202.0f/255.0f blue:202.0f/255.0f alpha:1.0]];
UIFont *font = [UIFont boldSystemFontOfSize:09.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
[mailboxsegment setTitleTextAttributes:attributes forState:UIControlStateNormal];
}
my code i will try to change background color and change font size
please try this one
- (void)segmentAction:(UISegmentedControl *)segment
{
UIColor *selectedColor = [UIColor whiteColor];
UIColor *deselectedColor = [UIColor colorWithRed: 54/255.0 green:52/255.0 blue:48/255.0 alpha:1.0];
for (UIControl *subview in [segment subviews])
{
if ([subview isSelected])
[subview setTintColor:selectedColor];
else
[subview setTintColor:deselectedColor];
}
}
You can create your custom segmented control. But its little complicated to create custom segmented control as you will have to provide images for selected and deselected states, 1px image for separator etc.
Instead of this i will suggest you to use four different buttons and apply logic to select only one button at a time.
To get effect of separator color, place these four buttons inside a stack view or a normal UIView and set background color of that container view to the color which you want to set for separator.

How to change the colour of back button?

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.

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