I'm having a trouble with a UITabBar with UITabBarItem added programmatically, the selected item is not being highlighted in white as usual.
Here's the source
UIImage *imageX = [UIImage imageNamed:#"sample.png"];
UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTitle:#"Sample" image:imageX tag:1];
NSArray *array = [NSArray arrayWithObjects:tabBarItem, nil];
[tabBar setItems:array];
When I select the item, the method didSelectItem of the delegate is called and the action is executed, but the image is not being highlighted by the bar...
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
Anyone know what's wrong ? I have to call other method to pass the highlighted image or something like that ?
The items of the tab bar are added programmatically but the tab bar I add via Storyboard.
Also, I did a test where the tab bar and the tab bar items are added via Storyboard, and everything works, including the highlighted icon is shown when it's selected
Thanks !
I solve the problem by adding code in - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item; function :
self.tabBar.selectedItem = item;
I don't know why I have to do this, but it's working.
Related
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:
I am using UITabbar because I like the Icon design. But I don't use UIButtons. I have two icon buttons and one label.
I've delegate method this. My NSLog won't appear if click first icon and second icon. I have this code appear warnings Xcode. How do you fix this?
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
if ([viewController.tabBarItem.title isEqualToString:#"FIRST"])
{
label.hidden = YES;
NSLog(#"FIRST");
} else if ([viewController.tabBarItem.title isEqualToString:#"SECOND"])
{
label.hidden = NO;
NSLog(#"SECOND");
}
}
In the code you posted, you need to replace the viewController.tabBarItem with just item both of the times it appears. The tab bar item that you want to investigate is the item parameter passed to this method. The method is not passed a viewController parameter, so the viewController variable is undefined. That is why XCode underlines it in red.
I want screen 1 to be shown when tabbaritem 1 is clicked and if I change some settings, i go to different view, , when I click the tabbaritem 1 again I want to show screen 2.
I have a UITabbar based app and the MainWindow.xib has different tabs loaded before with views.
How do I change it programmatically?
Please help
just put the code for the views to be created in the method viewWillAppear instead of viewDidLoad. This is being called each time go go back to your tab 1
Implement below method in your App Delegate
- (void)tabBarController:(UITabBarController *)controller willBeginCustomizingViewControllers:(NSArray *)viewControllers {
UINavigationController *nc;
nc = viewController;
if(controller.selectedIndex == 3){
[[nc.viewControllers objectAtIndex:0] replaceSubView];
}
}
Here,
if(controller.selectedIndex == 3)
3 = your viewcontroller index in which you want to change subview.
And "replaceSubView"
is your method in view controller in which you want to change subview.
Let me know in case of any difficulty.
Cheers.
Turn MainWindow or AppDelegate into UITabBarDelegate, than use
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
to caught tab selections. When tab you need is selected, use image property of UITabBarItem to set image you like.
I am trying to implement tabbar in my 2nd view.
I am able to place tabbar with 5 items on it. I know to handle those click events , i have to use tabbarcontroller.
My question is , taking tabbar on view,
How to call each item selected method without tabbarcontroller?
(My assumption is that tabbar is an object like button , and we can write a click method for that programmatically. So without Tab Controller also, we can access selected item method )
Is there any way to do that?
If you don’t want to use tabbarcontroller, then its always better to use Segmented Control.
It has similar kind of operations & its also simple to use. Use need to just create multiple uiviews on view & just play Hide-n-seek with them. If you want I have some code. Will paste here, if you want.
I think, you can do it like this. (*** Note : Its not tested code)
Add <UITabBarDelegate> in .h file
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if(item == firstItem)
NSLog(#"Did Select Here”);
else if(item == firstItem)
NSLog(#"Did Select Here”);
}
Create UITabbar and define UITabbarDelegate in .h file and write the following code in respective view:
- (void)viewDidLoad {
[super viewDidLoad];
UITabBarItem * newItem1 = [[UITabBarItem alloc] initWithTitle:#"First" image:[UIImage imageNamed:#"setting.png"] tag:1];
UITabBarItem * newItem2 = [[UITabBarItem alloc] initWithTitle:#"Second" image:[UIImage imageNamed:#"setting.png"] tag:2];
UITabBarItem * newItem3 = [[UITabBarItem alloc] initWithTitle:#"Third" image:[UIImage imageNamed:#"setting.png"] tag:3];
[tabbar setItems:[NSArray arrayWithObjects:newItem1,newItem2,newItem3,nil]];
tabbar.delegate = self;
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSLog(#"Tabbar selected itm %d",item.tag);
switch (item.tag) {
case 1:
//first selected
break;
case 2:
//second selected
break;
case 3:
//third selected
break;
default:
break;
}
}
I am using the same ViewController for several different views.
When instantiating the ViewController for a specific view, is there an easy way to specify the tab bar icon via code?
yourViewController.tabBarItem = [[UITabBarItem alloc]
initWithTitle:NSLocalizedString(#"Name", #"Name")
image:[UIImage imageNamed:#"tab_ yourViewController.png"]
tag:3];
The viewControllers are added to the tab bar, so the image and names should be set before the tab bar becomes visible (appDelegate if they are there on app start for instance). After that, you could use the above code to change the icon and text from the loadView or viewDidAppear within that viewController.
Yes. Your UITabBar has a property called items, which is an array of UITabBarItems for each tab bar item. You can create a UITabBarItem using the –initWithTitle:image:tag: constructor to use your own image, or the –initWithTabBarSystemItem:tag: constructor to use a system image.
You can also do this in the AppDelegate, by declaring a UITabBarController iVar and pointing it to the applications tabBarController. You can access the individual titles using the items array. and the setTitle.
#synthesize tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.tabBarController = (UITabBarController*)self.window.rootViewController;
NSArray* items = [self.tabBarController.tabBar items];
[[items objectAtIndex:0] setTitle:#"Home"];
[[items objectAtIndex:1] setTitle:#"Cool"];
[[items objectAtIndex:2] setTitle:#"Stuff"];
[[items objectAtIndex:3] setTitle:#"Settings"];
return YES;
}
Correct way is : Add this below line in viewDidLoad
[self.tabBarItem setImage:[UIImage imageNamed:#"<Image Name>"]];
to viewcontrollers which are set inside UITabBarController