how to run code in tabbarcontroller - objective-c

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 :)

Related

changing the text attributes of nav bar

i want to change the appearance of the nav bar, and so far i was able to change the background image of the nav bars, and also with the color.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions {
//set the bg image of all nav bars
UIImage *navBackgroundImage = [UIImage imageNamed:#"navigationBackground.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
return YES;
//customizing the title text of the nav bars
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:255.0/255.0 green:250.0/250.0 blue:240.0/240.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:#"Heiti TC" size:21.0], UITextAttributeFont, nil]];
}
this is the code i used to achieve changing the nav bar bg image and the color. if you look into the 2nd UINavigationBar appearance statement, i try to set the font of the nav bar with
[UIFont fontWithName:#"Heiti TC" size:21.0]
but it wont change the font. btw i run this with iphone 6.1 simulator on xcode 4.6.2. im am sure that the font name is "Heiti TC".
Maybe it doesn't work because you
return YES;
before changing the font?
The name supplied for the method fontWithName is not the same as the displayed family name of the font.
Try "STHeitiTC-Light" or "STHeitiTC-Medium" instead.
[UIFont fontWithName:#"STHeitiTC-Medium" size:21.0]
And put the
return YES
at the end of the didFinishLaunchingWithOptions.
PS: The answer here is quite helpful to find the font name to be used: Cant find custom font - iOS
Everything looks right. I have used similar code to achieve it as expected. Have you tried using the font Heiti TC Light or Heiti TC Medium?

Remove TabBar Button glow(not on image- hiding the selection tint of button)

I've created a tabbar application,I am using a custom TabBar with a backGround image.
I have finished all the parts but unable to remove the glow of tabbar button on click(i just changing the UIButton selection on click,but glow is still there)
How to hide the glow of UITabBar Buttons(ie hiding the selection tint of buttons) ?
Now TabBar is like this..
Need tabBar like this
You can use the following code.
[[UITabBar appearance] setSelectionIndicatorImage:[[UIImage alloc] init]];
Try It.
To change tabbar tint colour
[[UITabBar appearance] setSelectedImageTintColor:[UIColor grayColor]];
This will definitely help you. Simply create an UIImage object and pass it to setSelectionIndicatorImage property .
[yourTabbar setSelectionIndicatorImage:[[UIImage alloc] init]];
[self.tabBarController.tabBar insertSubview:[[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1.png"]] autorelease] atIndex:1];
You need to set UITabBarItem appearance:
[[UITabBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor yellowColor], UITextAttributeTextColor,
[UIFont systemFontOfSize:14.0f], UITextAttributeFont,nil]
forState:UIControlStateHighlighted];

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

Changing Text Color of Unselected UITabBarItems

Here's what I've tried:
I've tried the following in the parent view controller:
[self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil] forState:UIControlStateNormal];
I've tried the following in the application delegate:
[[UITabBarItem appearance]
setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil]
forState:UIControlStateNormal];
I've even tried changing UIControlStateNormal to any of the other available constants. The only one that changes anything is UIControlStateHighlighted which changes the color of the highlighted tab. Is this a bug in the API or is there something I'm missing?
Key things to note:
I'm using storyboarding
I have a UITabBarController where each tab has an embedded UINavigationController (pretty standard setup)
I've tried embedding the first code snippet into both the UINavigationController subclass as well as the root UIViewController subclass that is inside the UINavigationController. No luck there either.

change tabbar icon color from default blue

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.