change tabbar icon color from default blue - objective-c

i m trying to change the tabbar icon color from default blue to red...i m getting this error
"Stray '\342'in program".. i m getting the error at "-(void)recolorItemsWithColor:......." and also at the implementation section...is der anyway to solve dis error...is there anyother method to change the tab bar icon from default blue to some other color
#interface UITabBar (ColorExtensions)
– (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur;
#end

In the class where you define the tab bar set the property of the
tabBarItem to ->>
UITabBarItem *tabBarItem1 = [[self.tabBar.tabBar items] objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"campaigns_hover.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"campaigns.png"]];
Its a property of tabBarItem and u can change the default blue image to a custom image.
campaigns_hover.png is the selected custom image AND
campaigns.png is the custom image when not selected...
Enjoy the secret.. :)
It does not uses a private API.. The function is defined under UITabBarItem.h class.

go to your asset folder, find the asset and click on Identity Inspector , and change "Render As" to Original Image (assuming your icon is the color you want it)

Try adding a 49x49 png image into your project, then paste these line of code into your app delegate inside the applicationDidFinishLaunching and before adding a subview.
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *view = [[UIView alloc] initWithFrame:frame];
UIImage *tabBarBackgroundImage = [UIImage imageNamed:#"49x49.png"];
UIColor *color = [[UIColor alloc] initWithPatternImage:tabBarBackgroundImage];
[view setBackgroundColor:color];
[color release];
[[tabcontroller tabBar] insertSubview:view atIndex:0];
[view release];
Hope it will help.

Are you aware that the code you're trying to use uses private APIs and thus will cause your apps to be rejected?
I don't know about the specific error you're seeing. But if you're looking for another solution, one that'll make it into the App Store, you could try PESTabBarAdditions.

Related

UITabBar unselected icon tint

I am trying to change the color of my tab bar icons when the tabs are UNselected. Right now the color is default grey and I can change the color to whatever color I want for when it IS selected.
Apple's dev library said to change the image rendering to "original" instead of its default mode "template." I did that. then it says to use initWithTitle:image:selectedImage: I tried to do that as well but I think that's where I messed up. I wrote this in my viewcontroller.m file. What's wrong here?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *stat = [UIImage imageNamed:#"white_stats.png"];
stat = [stat imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
(instancetype)initWithTitle:(NSString *)nil image:(UIImage *)stat selectedImage:(UIImage *)stat;
}
The problem is that you are using the same UIImage with UIImageRenderingModeAlwaysOriginal in both places.
Your code should look something like
UIImage *stat = [UIImage imageNamed:#"white_stats.png"];
UIImage *statAlwaysOriginal = [stat imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:statAlwaysOriginal selectedImage:stat];
The other thing is that there are some actual syntax errors in your post (in the UITabBarItem initialization, but I suspect you just pasted it incorrectly.

how to run code in tabbarcontroller

Im trying to change the icon tint of my unselected tab bar icon images. I have used the patch code below, however, the post that i found this patch code in says to run this in the tab bar controller but i did not know how to do that so i ran it in the -(void)viewDidLoad method in the viewcontroller .m file. It came up with an error saying "Property 'tabBar' not found on object of type 'ViewController *'" How do i fix this?
// set color of selected icons and text to red
self.tabBar.tintColor = [UIColor redColor];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
// set color of unselected text to green
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil]
forState:UIControlStateNormal];
// set selected and unselected icons
UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
// this way, the icon gets rendered as it is (thus, it needs to be green in this example)
item0.image = [[UIImage imageNamed:#"unselected-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// this icon is used for selected tab and it will get tinted as defined in self.tabBar.tintColor
item0.selectedImage = [UIImage imageNamed:#"selected-icon.png"];
Instead of using that patch of code, try to do the below in your VC:
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"item_unselected.png"]];
Edit:
The code above is used when subclassing UITabBarController, so if you insis you need to subclass the 'UITabBarController', change the class of your tabBarController in the storyboard and put that block of code in its viewDidLoad.
You can delete your UIViewController from storyboard and add there UITabBarController and set it as Initial View Controller.
Then you can access this UITabBarController in AppDelegate.m like this:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
This is the fast way.
Also you can subclass UITabBarController, set this class in the storyboard and put all your code inside this subclass. (as null has said 15 minutes before :)

Visible buttons with transparent navigation bar

I've seen several apps that have completely transparent navigation bars but with visible buttons, I cant seem to find anything that wont make the button invisible as well. I'm sure they were using UINavigationController for the navbar because it had the same animations with fade and what not.
Im currently using this code in ViewDidLoad and ViewDidAppear to either hide or show the nav bar because it's not supposed to be on the first page-
[self.navigationController setNavigationBarHidden:NO animated:YES];
and this code for its transparency:
[self.navigationController.navigationBar setAlpha:0.0];
Create a subclass of UINavigationBar containing no methods except drawRect:. Put custom drawing code there if you need to, otherwise leave it empty (but implement it).
Next, set the UINavigationController's navigation bar to this subclass. Use initWithNavigationBarClass:toolBarClass: in code, or just change it in the Interface Builder if you're using storyboards/nibs (it's a subclass of your UINavigationController in the hierarchy at the side).
Finally, get a reference to your navigation bar so we can configure it using self.navigationController.navigationBar in the loadView of the contained view controller. Set the navigation bar's translucent to YES and backgroundColor to [UIColor clearColor]. Example below.
//CustomNavigationBar.h
#import <UIKit/UIKit.h>
#interface CustomNavigationBar : UINavigationBar
#end
//CustomNavigationBar.m
#import "CustomNavigationBar.h"
#implementation CustomNavigationBar
- (void)drawRect:(CGRect)rect {}
#end
//Put this in the implementation of the view controller displayed by the navigation controller
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.translucent = YES;
[self navigationController].navigationBar.backgroundColor = [UIColor clearColor];
}
Here's a screen shot of the result, mimicking Plague.
The blue border was drawn in drawRect: to show you that a UINavigationBar is there and not just a button and a label. I implemented sizeThatFits: in the subclass to make the bar taller. Both the button and label are UIView's containing the correct UI element that were placed in the bar as UIBarButtonItems. I embedded them in views first so that I could change their vertical alignment (otherwise they "stuck" to the bottom when I implemented sizeThatFits:).
self.navigationController.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
//remove shadow
[[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];
To make the Navigation Bar transparent, use the below code:
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.translucent = YES;
After this set the background image of the Navigation Bar the same as the view behind it using the below property:
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"SAMPLE.jpg"] forBarMetrics:UIBarMetricsDefault];

Custom View with UILabel in it appears black

I have created a custom UIView that has some set of UILabels in it. I add those UILabels inside the custom view inside its drawRect method.
But the whole custom view appears black on the screen. I have not set any background color for the custom UIView. How Do I fix this?
I tried setting background color to ClearColor but it still looks black.
I tried setting opaque property of the custom view to false and the view obviously disappeared.
Please help.
don't do that in drawRect: method which is intended to draw in the graphic context. If you want to add some specific subviews do it in an init / initWithFrame method.
For me the best way is to create a custom uiviewcontroller subclass and initialize it using a xib (nib) file. Working at controller level is a good practice.
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(10,0,320,35)];
newView.backgroundColor=[UIColor clearColor];
UILabel *mytext = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 28.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.text = #"Your label";
[newView addSubview:mytext];
[mytext release];
[self.view addSubview:newView];
[newView release];
Just incase someone stumbles upon this thread like I did in 2021. Check to see if you have accidentally toggled 'dark mode'. It will show similar visual 'issues' to the question above.

Set title/icon etc to TabBarItem created in IB, through code?

I have a TabBar that I've created through IB, I chose "create new project" -> "Tab bar application". Is there a way for me to access one of the TabBarItems for customization through the code?
It seems to me that something like:
[[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:#"Button one"];
should set the title of that item to "Button one", but it doesen't. The title itself is not a problem (I can set that through IB aswell), however adding an Icon seems to be.
So to sum up, what I really want to know is: Is there a way to add an Icon to a TabBarItem created through IB?
SOLUTION:
Adding in viewDidLoad in the first view, being loaded automatically upon starting the app:
UITabBarController *tb = [self tabBarController];
[[tb.tabBar.items objectAtIndex:1] setTitle:#"Title"];
Let me set the title of the second button (objectAtIndex: 1). I was also able to set the image the same way, which also worked for buttons one (objectAtIndex: 0) and three (objectAtIndex: 2).
Add this to your viewDidLoad: method of one of the tabBar viewControllers and it should work:
- (void)viewDidLoad
{
[super viewDidLoad];
//Get the tabBarItem
UITabBarItem *tbi = [self tabBarItem];
//Give it a lable
[tbi setTitle:#"Title A"];
//create a image from a file for the tabBar
UIImage *i = [UIImage imageNamed:#"NiceImage.png"];
//and put it on the tabBar
[tbi setImage:i];
}
You should be able to set the image and title properties on the TabBarItems:
UITabBarItem *item = (UITabBarItem *)[tabBarController.tabBar.items objectAtIndex:0];
item.image = [UIImage imageNamed:#"home.png"];
Don't forget that the UITabBar only uses the alpha values out of the image you set, so if you don't have an alpha channel in the image you may not see anything when you set an image on the tab bar item.
I've never created a tab bar through IB (always through code), however to set title and icon I use
controller.title = #"Controller";
controller.tabBarItem.image = [UIImage imageNamed:#"image.png"];
where controller is the UIViewController added to the viewControllers' array of the UITabBarController.