iOS 5: UITabBarItem setFinishedSelectedImage:withFinishedUnselectedImage: not working / ignored - iphone

As per Apple docs
I'm trying to set custom finished selected and unselected images on a UITabBarItem like so:
...
DetailViewController *vc1 = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
UITabBarItem *vc1i = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:100];
[vc1i setFinishedSelectedImage:[UIImage imageNamed:#"tab_bar_item_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab_bar_item_normal.png"]];
[vc1 setTabBarItem:vc1i];
...
Basically what's happening is the TabBar loads up just fine, it just completely ignores the tab bar item customization.
I'm targeting iOS5+
The images are 30x30 transparent PNGs and exist in the project. Can't figure out what I'm overlooking here, but must be something!
This is being called in the tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method, ala Ray Wenderlich's tutorial
Anyone have any ideas?
Thanks!

The tab bar item is initialized with the method: initWithTabBarSystemItem:tag:. But, as the documentation says:
This method returns a system-supplied tab bar item. The title and image properties of the returned item cannot be changed later.
You should initialize the tab bar item with initWithTitle:image:tag:.
UITabBarItem *vc1i = [[UITabBarItem alloc] initWithTitle:#"Top Rated" image:nil tag:100];
[vc1i setFinishedSelectedImage:[UIImage imageNamed:#"tab_bar_item_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab_bar_item_normal.png"]];

If your are trying to achieve displaying of the actual image at the UITabBar then use the following code.
[yourTabBarItem setImage:[[UIImage imageNamed:#"more.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
and if you want to display image in original condition for the selected then use the following
[yourTabBarItem setSelectedImage:[[UIImage imageNamed:#"more.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
these two are alternative to
setFinishedSelectedImage: withFinishedUnselectedImage:

Related

Change UI Tab bar Display color

I am new to ios. I want make app in tab bar
The tab bar colors now in this scren shot.
An the UI i want in my iphone is this image
IN each of icon of tab bar i used black images
Thanks in Advance
In your didFinishLaunchingWithOptions method in the app delegate you will need to add some code like this.
//Get the Tab Bar
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.delegate = self;
UITabBar *tabBar = tabBarController.tabBar;
//You may want to set the background of the tab bar (optional)
[tabBar setBackgroundImage:[UIImage imageNamed:#"CustomTabBar.png"]];
//You will need to repeat this code for each tab bar item
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
tabBarItem3.title = #"Settings";
tabBarItem3.image = nil;
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"settings-button-selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"settings-button.png"]];
Don't forget to make #2x images for retina support.
You can also change other settings for each tabBarItem - check out the docs to find out more.
You have to set each of your UITabBarItems separately using something like:
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:#"Title1" image:[UIImage imageNamed:#"image1.png"] tag:1];
[tabBarItem1 setFinishedSelectedImage:nil withFinishedUnselectedImage:[UIImage imageNamed:#"image1.png"]];
[[myTabBarController.viewControllers objectAtIndex:0] setTabBarItem:tabBarItem1];
for each of your view controllers.

Setting image on tabbar items in special case(if tab bar taken on viewController)

I am taking tab bar controller on view controller rather than delegate, and used code what mentioned below.
tabController = [[UITabBarController alloc]init];
tabController.delegate = self;
scanView = [[ScanTicketView alloc] init];
searchView = [[SearchView alloc] init];
historyView = [[HistoryView alloc]init];
tabController.viewControllers=[NSArray arrayWithObjects:scanView,searchView,historyView, nil];
[self.navigationController pushViewController:tabController animated:YES];
It works, but now how to apply images to these views. Can anyone help me here.
Thanks in advance.
You can use:
viewController.tabBarItem.image = [UIImage imageNamed:#"imageName.png"];
if you want to apply the image only to selected view controller in Tab bar, then UITabBarController has a selectedViewController and a selectedIndex property also see tabBarController.tabBar.selectedItem.tag if this may help.
Tabbar controller has its own property of Image and Title.
Go through this UITabbar Tutorial to understand properly. You can set image through property window or even through coding also.
Best Luck !

MoreNavigationController images disappearing on select

I have a UITabBarController which has been created programatically, which has 6 tabs. As such the MoreNavigationController is automatically created to take care of having more than 5 tabs. Everything looks fine when the MoreNavigationController is displayed, but when I select one of these rows to push the view controller on to the stack, the cell image (tab bar image) disappears. When I pop that view controller, the image remains hidden until the pop animation is completed, at which point the image suddenly appears again.
This is fairly old code and I wouldn't do it this way these days, but everything works except for this last little thing so I'm pretty hesitant to rip out all the code and do it another way. Can anyone suggest what I might be doing wrong?
An example of creating one of the tab bar view controllers:
InfoViewController* infoViewController = [[InfoViewController alloc] init];
infoViewController.tabBarItem.image = [UIImage imageNamed:#"90-life-buoy.png"];
infoViewController.tabBarItem.title = #"More Info";
infoViewController.title = #"More Info";
UINavigationController* infoNavController = [[UINavigationController alloc] initWithRootViewController:infoViewController];
[infoViewController release];
Creating the tab bar:
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:outdoorsNavController, peopleNavController, citiesNavController, landscapesNavController, infoNavController, basicsNavController, nil];
[window addSubview:tabBarController.view];
EDIT: Doesn't seem to make any difference whether I use retina (#2x) images or not.
The issue is because you're wrapping your InfoViewController in a UINavigationController.
When you click on the table row in MoreNavigationController, the controller uses the tabBarItem in UINavigationController while it does its transition. Because this is nil (in your code), the image in MoreNavigationController disappears. When the transition finally finishes, MoreNavigationController picks up the tabBarItem in InfoViewController
Try this:
InfoViewController* infoViewController = [[InfoViewController alloc] init];
infoViewController.tabBarItem.image = [UIImage imageNamed:#"90-life-buoy.png"];
infoViewController.tabBarItem.title = #"More Info";
infoViewController.title = #"More Info";
UINavigationController* infoNavController = [[UINavigationController alloc] initWithRootViewController:infoViewController];
//Set the tabBarItem for UINavigationController
infoNavController.tabBarItem = infoViewController.tabBarItem
[infoViewController release];
Here's a video reproducing and fixing the issue:
Item 7 has an empty tabBarItem.image while Item 6 has tabBarItem.image set
I'm not sure, but have you tried setting the NavigationCotroller's .tabBarItem?
Not sure if I understand correctly, but on the top of my head here are some suggestions (The More Tab Bar item shouldn't disappear at all times, that is created automatically)
Have you tried subclassing your UINavigationController that is being used for the MoreController's place and set its tab bar items properties there, therefore making sure you have control over its lifetime ?
Subclass your UIViewController's that you wish to push onto the navigation stack and have them use the same tab bar item ?
Mimic the default functionality, create your own UITableViewController to act as the More Controller, and at each tap of the rows, do what you want, also in a custom way.
PS: Try setting images name without the .png extension. This way you will automatically load the #2x resource as well. Eg: [UIImage imageNamed:#"90-life-buoy"]
Link

iPhone-SDK: pushViewController doesn't push UIWebView?

I have TabBarView Controller, where i keep four tab bar items, let sat item1, item2, item3 and item 4.
When clicking on item2 tab bar item, i call a RootViewController where it has a Navigation controller with a TableView and shows the row items.
Upto here it is fine.
When clicking on a row item, will have to launch a UIWebView to show the content.
I did code like below for this process.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
navigationController = [[UINavigationController alloc] init];
webViewController = [[WebViewController alloc] init];
[[self navigationController] pushViewController:webViewController animated:NO];
[webViewController release];
}
The problem now is, clicking on a row item is not pushing the UIWebView code at all. I tested the UIWebView code with normal addSubView method like
"[appDelegate.window addSubview:webViewController.view];"
instead of calling via pushViewController. It is calling UIWebView and shows the web content successfully.
I understand the problem should be calling as pushViewController:webViewController.
Can anyone point me what are the ways that it should be not be worked in this case and how can i correct it to make it work with pushViewController itself? I need to call UIWebView only using pushViewController.
For such all these combination, is there any sample source available?
Waiting for someone's help.
thanks.
Clave/
I think you are mistakenly creating a new navigation controller here
navigationController = [[UINavigationController alloc] init];
remove that line and use
[self.navigationController pushViewController:webViewController animated:NO];
instead of your reference above. That should do it :-)

iPhone dev - UIViewController title, tabBarItem, tag

I have a UITabBarController that manages 5 View Controllers. I create their tab bar items in their "init" methods so that they will be displayed before the view is loaded. I'm just wondering what way I should do it, because there seems to be so many ways. For example, for my DatePickerViewController:
- (id)init {
if((self = [super init])) {
// ================ THIS ==========================
UIImage *clockIcon = [UIImage imageNamed:#"clockicon.png"];
UITabBarItem *localTabBarItem = [[UITabBarItem alloc]
initWithTitle:#"Date" image:clockIcon tag:0];
[self setTabBarItem:localTabBarItem];
[localTabBarItem release];
// ================ OR THIS ========================
[self setTitle:#"Date"];
UITabBarItem *localTabBarItem = [[UITabBarItem alloc] init];
[localTabBarItem setImage:[UIImage imageNamed:#"clockicon.png"]];
[self setTabBarItem:localTabBarItem];
[localTabBarItem release];
// ================ OR THIS ========================
UITabBarItem *localTabBarItem = [[UITabBarItem alloc] init];
[localTabBarItem setTitle:#"Date"];
[localTabBarItem setImage:[UIImage imageNamed:#"clockicon.png"]];
[self setTabBarItem:localTabBarItem];
[localTabBarItem release];
}
return self;
}
Which way should I do it? And why is there a title for both the tabBarItem and the View Controller? And I don't think I need the tag (which is set in the first method).
Thanks!!
Well in my opinion any of these ways are ok, it might be more readible when you declare the UIImage in one line and set it in a different line rather than doing it all inline, but at the end you get the same result.
The TabBarItems have a title which is the text that will show in the tab bar item iteself. View Controllers have a title for Navigation Controller purposes, the View Controllers title is displayed in the NavigationControllers NavBar when set. And you do need tags, tags is the way you tell the buttons apart when someone click on them (when u manage the TabBar on your own).
The reason there are several ways to set the title is for convienece. You may want to display one title in the navigation bar and one title in the tab bar.
This actually quite common since there is less space to display text in the tab bar.
Like many things in Cocoa, there is more than one way to do it. The only "correctness" you need to be concerned about is what works best for your situation.