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.
Related
I have a tab controller with four items in it. Second item is a navigation controller with a table view. I want to place logo of my app instead of title. Finally I can show my logo, but I can't understand how to scale it down to the size of Navigation Bar. Size of my image is 59x68px.
I do the followings thing in MyTableViewController viewDidload
[super viewDidLoad];
self.navigationController.navigationBar.translucent= NO;
UIImage *image = [UIImage imageNamed:#"assorti_logo.png"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:image];
myImageView.contentMode = UIViewContentModeScaleAspectFill;
self.navigationItem.titleView = myImageView;
I tried a lot of solutions from here, and non works. Is there any way to scale this image to match navigation bar height. Or what size of image should I use to see correct image both on iPhone 4s and 5?
I've finally found out the problem. I needed only to rename my image file to "logo#2x.png" The key is "#2x" in the end.
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.
I have root ViewController and detailed ViewController. When i push to detailedViewController i get leftBarButtonItem with the title from the root one. But i want the title to be just "Back", nothing more. So how to do that?
This doesn't help
self.navigationItem.leftBarButtonItem.title = #"Back";
To create my on type barButtonItem(for example 104 with left arrow) and to set it to leftBarButtonItem is terrible decision.
Is there other way than to change the title of the rootViewController manually before pushing?
From Apple's doc:
backBarButtonItem
The bar button item to use when a back button is needed on the
navigation bar.
#property(nonatomic, retain) UIBarButtonItem *backBarButtonItem
Discussion
When this navigation item is immediately below the top item in the
stack, the navigation controller derives the back button for the
navigation bar from this navigation item. When this property is nil,
the navigation item uses the value in its title property to create an
appropriate back button. If you want to specify a custom image or
title for the back button, you can assign a custom bar button item
(with your custom title or image) to this property instead. When
configuring your bar button item, do not assign a custom view to it;
the navigation item ignores custom views in the back bar button
anyway.
So, you can create create your barButtonItem (e.g. – initWithTitle:style:target:action:) and assign it to that property.
In addition, if you want to have a custom image for UIBarButtonItem (left or right) I suggest you to create a category extension like the following:
//UIBarButtonItem+Extension.h
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString*)title target:(id)target action:(SEL)action;
//UIBarButtonItem+Extension.m
+ (UIBarButtonItem*)barItemWithImage:(UIImage*)image title:(NSString*)title target:(id)target action:(SEL)action
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.titleLabel.textAlignment = UITextAlignmentCenter;
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
return [barButtonItem autorelease];
}
and then use it as
UIBarButtonItem* backBarButtonItem = [UIBarButtonItem barItemWithImage:[UIImage imageNamed:#"YoutImageName"] title:#"YourTitle" target:self action:#selector(doSomething:)];
I finally figured out all the wrinkles in this today, and it's simpler then above.
The child's back button text is based on values set in its parent. This is obvious behaviour, when you think about it: If a view controller can be reached from two parents, the back button's text should depend on which pushed it.
If the text is always the same:
Select the parent view controller's Navigation Item in the editor.
Put the text into the Back Button value.
And like that you're done. When this view controller is pushed aside by a new view controller, that new view controller will get this text as its title.
If the text is dynamic:
Select the parent view controller's Navigation Item in the editor.
Put some text into the Back Button value.
Set the title when it should change in the parent view controller: self.navigationItem.backBarButtonItem.title = dynamicText;
And, again, you're done.
To be clear, you can set this at any time in the parent view controller. It will only be shown when another view controller is pushed.
If you don't put the text in the Back Button in the designer, the process of instantiating the view controller won't create self.navigationItem.backBarButtonItem, and you can't set the title of a nil object. I believe this is where all of the confusion around this stems from.
Of course, you can create this at runtime, but if you're already doing most of your work in the storyboard/nib it's easier to let the decoder do it for you.
If you're more curious about this, I just wrote a blog post on the subject as well. It has some more details.
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] init];
backButtonItem.title = NSLocalizedString(#"Back", nil);
self.navigationItem.backBarButtonItem = backButtonItem;
It works for me:
[self.navigationItem.leftBarButtonItem setTitle:#"Back"];
(back arrow image and custom text)
I can't figure out how to change the bottom bar titles on the Tab Bar.
Please see this image:
Assuming you meant you cannot figure it out, and you want something like in the image,
tabBarController.tabBarItem.title = #"Your title"
In each view controller that goes into the bottom tabbar, you must specify the tab bar item:
insert this code in your init method, or viewDidLoad method.
UITabBarItem * tabtitle = [[UITabBarItem alloc] initWithTitle: #" YOUR TITLE "
image: nil //or your image
tag: 0];
[self setTabBarItem: tabtitle];
//[tabtitle release]; //release if not using ARC
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.