UISegmentedControl only changes text color when revisiting ViewController - ios

UPDATE ANSWERED. BY ME.
Currently having problems with the text color change of my UISegmentedControl; it needs to change on first load with UIControlStateSelected. Code works, but only conditionally. It works when you visit the page with the segmented control on the navigation bar, hit the back button, and then visit the page again. I'm assuming there's a problem with inheritance here. Let me explain..
The location of the the segmented control lies on top of my navigation bar.
Inheritance of the ViewController which contains the SegmentedControl:
TabBarViewController(managed with AppDelegate)-->navigation Controller-->ViewController(where 'inviteSegBar' lies)
Here's the code within AppDelegate.m:
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithHexString:#"#669900"]];//this one sets it green.
[[UINavigationBar appearance] setBackgroundColor:[UIColor whiteColor]];
And here's the viewDidLoad: code for the VC which contains 'inviteSegBar', the UISegmentedControl in question:
- (void)viewDidLoad
{
[super viewDidLoad];
//CUSTOM APPEARANCE <below>
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:#"#669900"];
inviteSegBar.tintColor = [UIColor colorWithHexString:#"#333333"];
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithHexString:#"#669900"]} forState:UIControlStateSelected];
}
Like I said the last line works, but only when you re-visit the page. Why is this happening?
PS This is the same issue guys, I had already tried this code before any of the answers were listed.

ANSWER FOUND: Simply move
[[UISegmentedControl appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor colorWithHexString:#"#669900"]} forState:UIControlStateSelected];
to your AppDelegate.m file

Use
UIColor *whitecolor = [UIColor whiteColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[whitecolor] forKeys:#[UITextAttributeTextColor]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateNormal];
UIColor *grayColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[grayColor] forKeys:#[UITextAttributeTextColor]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateSelected];
update
UIColor *whitecolor = [UIColor whiteColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[whitecolor] forKeys:#[NSForegroundColorAttributeName]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateNormal];
UIColor *grayColor = [UIColor darkGrayColor];
NSDictionary *attributes = [NSDictionary dictionaryWithObjects:#[grayColor] forKeys:#[NSForegroundColorAttributeName]];
[yourSegment setTitleTextAttributes:attributes
forState:UIControlStateSelected];

This code allows you to set some text attributes for label in segmented control:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blackColor], UITextAttributeTextColor,
nil];
[_segmentedControl setTitleTextAttributes:attributes forState:UIControlStateSelected];
More allowed attributes in Apple documentation: link

This may help you:
UIAppearance proxy to set title text attributes but preserve tintColor for borders.
[[UISegmentedControl appearance] setTitleTextAttributes:#{
NSForegroundColorAttributeName : [UIColor redColor]
} forState:UIControlStateNormal];

For change UISegmentedControl appearance insert for example into viewDidLoad function this code:
// color selected text ---> red
[[UISegmentedControl appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor redColor] } forState:UIControlStateSelected];
// color disabled text ---> blue
[[UISegmentedControl appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor blueColor] } forState:UIControlStateNormal];
// color tint segmented control ---> black
[[UISegmentedControl appearance] setTintColor:[UIColor greenColor]];

Related

UINavigationBar back button item font color won't set

I have a navigation bar on a viewController that I can enable/disable. The problem is I can't get the font for the UIBarButtonItems to change colors after the view initially loads, though the back arrow will change.
I disable the UINavigationBar on myViewController with the following line of code:
self.navigationController.navigationBar.userInteractionEnabled = NO;
I have UIControlStates configured in AppDelegate.m for UIBarButtonItems for enabled and disabled, but nothing happens to the font after the view initially loads.
In my AppDelegate.m, I have the following code to set the UINavigationBar's font and color:
// UIBarButtonItem styling
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];
// enabled
NSDictionary *enabledTextAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor customCellHyperlinkColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:17.0]};
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:enabledTextAttributeDictionary forState:UIControlStateNormal];
// disabled
NSDictionary *disabledTextAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:17.0]};
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:disabledTextAttributeDictionary forState:UIControlStateDisabled];
// UINavigationBarTitle styling
NSDictionary *titleAttributeDictionary = #{NSForegroundColorAttributeName : [UIColor blackColor], NSShadowAttributeName: shadow, NSFontAttributeName:[UIFont fontWithName:#"GillSans" size:19.0]};
[[UINavigationBar appearanceWhenContainedIn:[UINavigationController class], nil]setTitleTextAttributes:titleAttributeDictionary];
I thought since I configured the 2 states (UIControlStateNormal/UIControlStateDisabled) in AppDelegate.m, nothing needs to be done other than enable/disable the navigationBar. Enable/disable does indeed enable/disable, but the color on the back button label doesn't change (though it does initially set to the color I set for UIControlStateNormal in AppDelegate).
I tried to manually set it, but the label on the backButtonItem stays blue while the icon to the left of it tints light gray. What (obvious thing) am I missing that is preventing me from changing the color of my back button font?
I think you are looking for this
Add this code in you AppDelegate.m
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor clearColor];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor redColor],
NSShadowAttributeName:shadow,
NSFontAttributeName:[UIFont systemFontOfSize:13.0]
}
forState:UIControlStateNormal];
I think you need to use
[self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];
self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : place your font here}];
instead of
self.navigationController.navigationBar.tintColor = [UIColor lightGrayColor];

Change color of inactive tab-bar icons

I try to change color of my tab bar items, because it always grey in unactive and blue in active.
So, after some searching I try to write this code it all my ViewControllers for Tab bar
self.tabBarItem.selectedImage = [[UIImage imageNamed:#"TabBarItemMenu_tabbed.png"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem.image = [[UIImage imageNamed:#"TabBarItemMenu.png"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
But it doesn't help me, and I always get
You can do this in addition:
Set tintColor attribute of the tab bar to set the color of the selected icon
self.tabBar.tintColor = [UIColor redColor];
Then you can use text attributes to recolor the text
for (UITabBarItem *item in self.tabBar.items) {
NSDictionary *normalState = #{UITextAttributeTextColor : [UIColor colorWithWhite:1.000 alpha:1.000],
UITextAttributeTextShadowColor: [UIColor clearColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0, 1.0)]};
[item setTitleTextAttributes:normalState forState:UIControlStateNormal];
NSDictionary *selectedState = #{UITextAttributeTextColor : [UIColor redColor],
UITextAttributeTextShadowColor: [UIColor clearColor],
UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0, 1.0)]};
[item setTitleTextAttributes:selectedState forState:UIControlStateHighlighted];
}
// Edit
Since upper code is deprecated for iOS7, here an update:
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateSelected];

Changing the font in a UITabBar

I was wondering if someone would be able to help me with a problem I am having at the moment. I a m trying to change the actual font of the UITabBar, I have been able to chance the text colour and the colour when the text is selected. The code i have so far is here:
//Setting tab bar background
UIImage* tabBarBackground = [UIImage imageNamed:#"tabbar.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
//[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"selected.png"]];
// Changes Selected Image color
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:33.0/255.0 green:35.0/255.0 blue:38.0/255.0 alpha:1.0]]; //Icon colour
// Changes StateNormal text Color,
[UITabBarItem.appearance setTitleTextAttributes: #{NSForegroundColorAttributeName : [UIColor colorWithRed:214.0/255 green:69.0/255 blue:65.0/255 alpha:1.0]} forState:UIControlStateNormal]; //Colour of text when not seleted
// Changes StateSelected
UIColor *titleHighlightedColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0]; //Colour of text when selected
[UITabBarItem.appearance setTitleTextAttributes: #{NSForegroundColorAttributeName : titleHighlightedColor} forState:UIControlStateSelected];
I was wondering if anyone would know if it is possible to change the font here and if so how i would come across doing so? The code is located in the AppDelegate.m
Thanks in advance,
Harrison
Try adding it to the title text attributes:
[UITabBarItem.appearance setTitleTextAttributes: #{
NSForegroundColorAttributeName : [UIColor colorWithRed:214.0/255 green:69.0/255 blue:65.0/255 alpha:1.0],
NSFontAttributeName : [UIFont systemFontOfSize:20]
} forState:UIControlStateNormal];
You could use
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"Arial" size:5.0f], NSFontAttributeName, nil] forState:UIControlStateNormal];

iOS 7 UINavigationBar appearance not working first timeā€¦

I am trying to change the look of the UINavigationBar in my iOS7 app. I am doing the following:
- (void)viewDidLoad
{
[super viewDidLoad];
m_sNumberToCall = #"";
UIBarButtonItem * btn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"IconHome.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(btHomeTouched:)];
self.navigationItem.leftBarButtonItem = btn;
self.navigationController.navigationBar.translucent = YES;
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8];
shadow.shadowOffset = CGSizeMake(0, 1);
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0],
NSForegroundColorAttributeName,
shadow,
NSShadowAttributeName,
[UIFont fontWithName:#"Helvetica-Bold" size:21.0],
NSFontAttributeName,
nil]];
}
But, the first time I present the UITableViewController it is the standard iOS7 nav bar, then I press home and present it again and it is my new look.
Any ideas why it does not work the first time?
Don't change the appearance but the navigation bar directly. The appearance affects only the future instances but not the already created ones.
Change:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
to:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"TVCNavBack.png"] forBarMetrics:UIBarMetricsDefault];
The answer before only helps you with the background image but not with the title text attributes.
You don't need to change your code but all you have to do is move it to
applicationDidFinishLaunchingWithOptions
in your AppDelegate.m file.

change tabbar text color,iPhone

I have created a tabbar programmatically.
Can we change color of title of the tabbar item?
Default is white, and i m trying to make it black.
something like
tabbaritem.text.textcolor=[UIcolor Blackcolor];
Thanks
In iOS5 you use the appearance proxy to change the title color:
For a specific tabbar item:
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"AmericanTypewriter" size:20.0f], UITextAttributeFont,
[UIColor yellowColor], UITextAttributeTextColor,
[UIColor redColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset,
nil] forState:UIControlStateNormal];
Or replace self.tabBarItem with [UITabBarItem appearance] to change all your tabbar items.
If you want to change them all , I suggest you place the code on appdelegate didFinishLaunchingWithOptions:
Just something else ...
To set your standard appearance in iOS 5:
In your AppDelegate.m do:
[[UITabBar appearance] setTintColor:myColor]; //or whatever you want to change
Saves you a lot of work.
Maybe this updated code helps someone:
[UITabBarItem.appearance setTitleTextAttributes:#{
NSForegroundColorAttributeName : [UIColor lightGrayColor] } forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:#{
NSForegroundColorAttributeName : [UIColor whiteColor] } forState:UIControlStateSelected];

Resources