How to set a different unselected image and text color on UITabBarItem - ios

I'd like to set different colors for a UITabBarItem's title text and image in the unselected state.
For the selected state, I can accomplish this like so:
[[UITabBar appearance] setTintColor:[UIColor purpleColor]]; // image color
[[UITabBarItem appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName: [UIColor orangeColor] } forState:UIControlStateSelected]; // text color
For the unselected state, I'm attempting the following:
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blueColor]]; // image color
[[UITabBarItem appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName: [UIColor redColor] } forState:UIControlStateNormal]; // text color
But for some reason, the unselectedItemTintColor setting overrides whatever I try and set for the titleTextAttributes – so in the snippet above, both the text and image would appear blue.
I've also tried changing the titleTextAttributes directly on the UITabBarItem after I've created it (instead of using appearance), but again this seems to have no effect.
How can I achieve different unselected colors? Is it possible?

I managed to solve this shortly after posting. It turns out that while setting the unselectedItemTintColor using UIAppearance overrides the titleTextAttributes for the item, everything works correctly if you set the unselectedItemTintColor directly on the tab bar itself.
So instead of
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blueColor]];
just do
[self.myTabBarInstance setUnselectedItemTintColor:[UIColor blueColor]];

Related

Why my tabbar was rendered with gray rect?

// Tabbar
// Icon inactive
[[UITabBarItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateNormal];
// Icon active
[[UITabBar appearance] setTintColor:PRIMARY_COLOR];
// Text active
[[UITabBarItem appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName: PRIMARY_COLOR} forState:UIControlStateSelected];
// Background
[[UITabBar appearance] setBarTintColor:WHITE_COLOR];
// Over View YES/NO
[[UITabBar appearance] setTranslucent:NO];
I've a gray rect around the text and the icon.
Why? Where my damn mistake?
After discussion, we find that #CeccoCQ used another setting for UIView. That makes every view have a custom background color (in the image, it's gray color).
[[UIView appearance] setBackgroundColor:WINDOW_BACKGROUND_COLOR];
To resolve the problem, remove this custom setting and everything will work fine.

In a UISegmentedControl, how do I set the normal color separate from the selected color?

This is the effect that I'm going for:
This is what I have so far:
Cocoa does not allow you to set both an icon and text for each segment, so I was forced to burn the text into an image:
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:#[
[UIImage imageNamed:#"segmentedControlContacts"],
[UIImage imageNamed:#"segmentedControlOtherApps"]
]];
The last thing I have to finish is making the normal (unselected) segment gray instead of its selected/tint blue color. The following did not work:
// themeColor is defined as a shade of blue in a category
[UIView appearance].tintColor = [UIColor themeColor];
[[UISegmentedControl appearance]
setBackgroundImage:[UIImage imageNamed:#"segmentedControlEdgeNormal"]
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance]
setBackgroundImage:[UIImage imageNamed:#"segmentedControlEdgeSelected"]
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance]
setTitleTextAttributes:#{
NSForegroundColorAttributeName: [UIColor grayColor]
}
forState:UIControlStateNormal
];
The line
[[UISegmentedControl appearance] setTitleTextAttributes:#{
NSForegroundColorAttributeName: [UIColor grayColor]
}
forState:UIControlStateNormal
];
Will only works for title of segment control. If you remove images from segment and add title for both segment than this will surely works.
This will not take an effect on images for segment control.
For image customisation you must have to use method:
- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics
Also it will not show tint colour as you expected.
Suggestion:
If you still insist to make that custom behaviour, I suggest leave segment control instead create two buttons and place them side by side to feel like a segment control.
And for that behaviour you need grey image for button background and change for alternate button events.

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];

Change tintColor of unselected UITabBarController item title and background image

How can I change the tintColor of an unselected UITabBarItem title and background image iOS 8?
The default color for an unselected state is a light gray color, but it does not show on my darkish shade UITabBar background
I'd like my unselected state to have a color of [UIColor blackColor]
Inside my app delegate didfinishlaunchingwithoptions: I have
UIImage *deselectedE = [[UIImage imageNamed:#"mincraft_axe_green_32.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
UIImage *selectedE = [[UIImage imageNamed:#"mincraft_axe_green_32.png"] imageWithRenderingMode:UIImageRenderingModeAutomatic];
e.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Profile" image:deselectedE selectedImage:selectedE];
[[UITabBar appearance] setTintColor:[UIColor blackColor]];
Figured it out!
Use this to change the color of the text:
[[UITabBarItem appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor greenColor] }
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor blackColor] }
forState:UIControlStateSelected];
And make sure that image rendering mode is set to ORIGINAL for the images
UIImage *deselectedImage = [[UIImage imageNamed:#"deselectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *selectedImage = [[UIImage imageNamed:#"selectedImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
In your AppDelegate.m inside of application didFinishLaunchingWithOptions: use the following code:
//unselected icon tint color
[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];
//selected tint color
[[UITabBar appearance] setTintColor:[UIColor greenColor]];
//text tint color
[[UITabBarItem appearance] setTitleTextAttributes:#{ NSForegroundColorAttributeName : [UIColor whiteColor] }
forState:UIControlStateNormal];
//background tint color
[[UITabBar appearance] setBarTintColor:[UIColor blueColor]];
You can also render the image as original from the attributes inspector for the asset file without writing any code
You can also set it up directly in Storyboard... Check my answer here:
How to set UITabBarItem's unselected tint, ***including system items*** (iOS7)
If you're using Storyboard you can also set both Image for Bar Item and Selected Image for Selected Bar Item to get unaltered image in tabBar.
Alternatively in Assets catalog, you can select Render As: Original Image in the attributes of your image (View > Utilities > Show Attributes Inspector or shortcut ⌥⌘4 (Option + Command + 4))

How to set UISwitch border color?

My app has:
[[UIView appearance] setTintColor:[UIColor whiteColor]];
And here is what I have when on:
and off:
I need to make UISwitch border visible like in Settings.app:
Your [[UIView appearance] setTintColor:[UIColor whiteColor]]; is interfering with the tint color of your switch. The command to set the tint color is self.mySwitch.tintColor = [UIColor grayColor]; which sets the color used to tint the outline of the switch when it is turned off.
Will you please try adding this line to your AppDelegate's didFinishLaunchingWithOptions
[[UISwitch appearance] setTintColor:[UIColor grayColor]];
This should apply the chosen Tint color on all your UISwitch controls.
Rather than using the appearance proxies you can also use:
[self.mySwitch setThumbTintColor:[UIColor blueColor]];
[self.mySwitch setOnTintColor:[UIColor redColor]];
ie. Use setOnTintColor for the background/border color.

Resources