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
Related
Currently, I am running with the problem while applying the selected image over UITabBarButton. When I am doing this it changes the hierarchy of UITabBarbutton within tabBar.items. Really don't have any idea why this is happening. Posting the code-
UIWindow *keyWindow = [[UIApplication sharedApplication]keyWindow];
UITabBarController *tabBarController =(UITabBarController*)[keyWindow rootViewController];
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *targetTabBarItem = [[tabBar items] objectAtIndex:1]; // whichever tab-item
[targetTabBarItem setSelectedImage:image];
[targetTabBarItem setImage:image];
What I am doing here is-
Getting the image from the server and applying it over the particular UItabBarbutton image. Whenever I set it, it takes the UITabbarButton and inserts at the last position of all the UITabBarButtons.
Whenever image got changed from the server it's hierarchy got changed somewhat like this-
Here we can see that initially, the order of buttons was Tab1, Tab2 and Tab3, while after changing the selected image it got changed into Tab1, Tab3 and Tab2. It is indicating that the selected image index pops out and always inserts to last in TabBarButtons collection. If I change the selected image at index 0 initially the order would be Tab2, Tab3 and Tab1.
So anyone here can give a brief idea which is going wrong in the code?
Sorry for the long post and thanks in advance as well.
Thanks.
The ordering of the tab bar view hierachy as displayed in the view debugger should be totally independent from the backing tabBarController's tabBar property which is holding the tab bar item array.
My guess is the reason you are seeing this adjustment of the ordering the view hierarchy is because changing the image property will either cause it to be redrawn or removed/readded to the view hierarchy so it will then be the last item added there (this should not at all affect the ordering of the tabBarController.tabBar.items)
You'll see the same thing occur if you just change the title of a tab bar item.
I rigged up a button to change the index 1 tab bar item image similar to the code you show, then display the tab bar ordering in a label:
- (IBAction)changeTabBarImage:(id)sender {
UIWindow *keyWindow = [[UIApplication sharedApplication]keyWindow];
UITabBarController *tabBarController =(UITabBarController*)[keyWindow rootViewController];
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *targetTabBarItem = [[tabBar items] objectAtIndex:1]; // whichever tab-item
NSMutableString *tabBarOrder = [[NSMutableString alloc] init];
for (NSUInteger tabBarIndex = 0; tabBarIndex < [tabBar items].count; tabBarIndex++) {
[tabBarOrder appendString:[NSString stringWithFormat:#"index: %lu title: %#\n", tabBarIndex, [[tabBar items] objectAtIndex:tabBarIndex].title]];
}
self.firstLabel.text = [NSString stringWithString:tabBarOrder];
UIImage *image = [UIImage imageNamed:#"sync"];
[targetTabBarItem setSelectedImage:image];
[targetTabBarItem setImage:image];
}
The only image ever changed is the tab bar at index 1 and the ordering of the tabBarController.tabBar.items remains the same, even though the view hierarchy ordering in the view debugger changes (as you show in your screenshots).
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaViewsGuide/WorkingWithAViewHierarchy/WorkingWithAViewHierarchy.html
If you're seeing something different, i'd recommend adding some additional code, as I'm not able to reproduce the issue with just what you've aded.
Hi I'm new in objective C and using navigation controller in my app. Where I want to set an icon image and title of screen. like
How can I do this. I've used below code but it does not work as I want. It shows only image in navigation Bar.
self.navigationController.title = NSLocalizedString(#"Concate Your Memories", nil);
UIImageView* tileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"TopBar-icon.png"]];
tileImageView.frame = CGRectMake(0, 0, 60, 25);
self.navigationItem.titleView = tileImageView;
Please help me. Thanks in advance
The code you have given and everyone else have suggested is correct. But you're assigning the image and title separately. Therefore,
Create your own view with your image and a label and assign it as the title view OR
Add the title to the image itself and assign it to the titleview like as you are doing now
For Setting Image in Navigation Bar
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"TopBar-icon.png"] forBarMetrics:UIBarMetricsDefault];
that code works properly on iOS7:
[self.navigationItem setTitleView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo.png"]]];
(tested on real device and simulator as well)
You can create your own view and set it on the top of screen.
Hide default navigation bar for the whole app.
I have a navigation bar to which I have added right BarButton successfully. Both the NavigationBar and BarButton are created programmatically. Now according to my requirement I got to add two right BarButtons to my navigation Bar. Can anyone tell me how to do this? My app is targeting ios4.
This code will do the trick for you,
NSArray *barButtonItems= [[NSArray alloc] initWithObjects:self.addButton,self.sortbyButton,nil];
self.navigationItem.rightBarButtonItems=barButtonItems;
where addButton and sortbyButton are 2 separate BarButton Items
I know it is too late but I faced it recently. So here is what I did
Create a UIView in code and add the buttons as subviews into this view.
Create a ToolbarButton using [[UIBarButtonItem alloc] initWithCustomView:buttons]
Assign this toolbar button as the Left or right barbuttonItem as U wish.
If you application is targeting iOS 4 and above then you should take UISegmentControl and have two segments on it. Catch value changed action event and check which segment is selected and do you operation accordingly.
You can also set images to segments to make look and feel better.
As the documentation to UINavigationItem1 describes, it has a property rightBarButtonItems (as well as leftBarButtonItems) where you can give an array of UIBarButtons. They are display from right (index 0) to left (index n-1).
#Matthias, As stated in documentation, rightBarButtonItems property is available from iOS 5 and above and this function needs to be supported also on iOS 4.
So, UISegmentControl is best way to achieve this.
NSArray *segmentTextContent = [NSArray arrayWithObjects:
NSLocalizedString(#\"Group By\", #\"\"),
NSLocalizedString(#\"Filter By\", #\"\"),
nil];
UISegmentedcontrol *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 125, 30);
[segmentedControl addTarget:self action:#selector(toggleUnit) forControlEvents:UIControlEventValueChanged];
segmentedControl.tintColor = [UIColor lightGrayColor];
defaultTintColor = [segmentedControl.tintColor retain];
self.navigationItem.rightBarButtonItem = segmentedControl;
[segmentedControl release];
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 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.