i am using tabbarcontroller in my project. Here is my storyboard
In all view controllers tabbar item, I set images like below
I make a class of tabbarcontroller and do coding below
UITabBar *tabBar = self.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];
// [tabBarItem1 setImage:[[UIImage imageNamed:#"ic_home_tab_home_sel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
// [tabBarItem1 setSelectedImage:[[UIImage imageNamed:#"ic_home_tab_home_sel"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"ic_home_tab_home_sel"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_home_tab_home"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"ic_home_tab_profile_sel"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_home_tab_profile"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"ic_home_create_post_sel"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_home_create_post"]];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:#"ic_home_tab_chat_sel"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_home_tab_chat"]];
[tabBarItem5 setFinishedSelectedImage:[UIImage imageNamed:#"ic_home_tab_setting_sel"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_home_tab_setting"]];
But When I run it, First time all images are mixed, But when I clicked them one by one like in Video then all images sets.I dont know why this happend? Please help meThank you
I also faced same problem then I used below code in appdelegate
UINavigationController *navigationVC1 = [[UINavigationController alloc]initWithRootViewController:viewController1];
navigationVC1.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"My Trips"
image:[UIImage imageNamed:#"cab.png"]
tag:1];
UINavigationController *navigationVC2 = [[UINavigationController alloc]initWithRootViewController:viewController2];
navigationVC2.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"My Children"
image:[UIImage imageNamed:#"kids-couple.png"]
tag:2];
try like this.
Related
I have uiViewControllers embedded in uiNavigationControllers that are driven by a TabBarController. Majority of the time when I move between tabs everythings fine, but at random times when I jump to the first tab the screen goes blank (black). The tabBar is still visible and I'm able to jump to the other two tabs, but the first one remains blank. This has only happened on my iPhone and not in simulator. My appdelegate looks like below code, any ideas?
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
tabBarItem1.title = #"Tab 1";
tabBarItem2.title = #"Tab 2";
tabBarItem3.title = #"Tab 3";
[tabBarItem1 setImage:[[UIImage imageNamed:#"tab1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem1 setSelectedImage:[[UIImage imageNamed:#"tab1a.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setImage:[[UIImage imageNamed:#"tab2a.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem2 setSelectedImage:[[UIImage imageNamed:#"tab2a.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setImage:[[UIImage imageNamed:#"tab3.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem3 setSelectedImage:[[UIImage imageNamed:#"tab3a.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:207/255.0 green:83/255.0 blue:0/255.0 alpha:1.0]];
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:207/255.0 green:83/255.0 blue:0/255.0 alpha:1.0]];
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTintColor:[UIColor whiteColor]];
[UINavigationBar appearance].titleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
UINavigationController *firstVCnav = [[tabBarController viewControllers] objectAtIndex:0];
UINavigationController *secondVCnav = [[tabBarController viewControllers] objectAtIndex:1];
UINavigationController *thirdVCnav = [[tabBarController viewControllers] objectAtIndex:2];
MyFirstVC *oneVC = [[firstVCnav viewControllers] objectAtIndex:0];
oneVC.managedObjectContext = self.managedObjectContext;
MySecondVC *twoVC = [[secondVCnav viewControllers] objectAtIndex:0];
twoVC.managedObjectContext = self.managedObjectContext;
MyThirdVC *thirdVC = [[thirdVCnav viewControllers] objectAtIndex:0];
thirdVC.managedObjectContext = self.managedObjectContext;
return YES;
From another forum I received a tip to check if I'm using a nib where the view controller setup is preconfigured.
I did have a nib reference in a viewcontroller since I removed it the issue hasn't occured, but since random/rare issue I'll monitor and update if issue comes back.
I am using UITabBar in iOS app. I have set images to UITabBar items. Code is-
self.mainTabBar.delegate = self;
mainTabBar.tintColor = nil;
[[mainTabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:#"ic_1.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_1.png"]];
[[mainTabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:#"ic_2.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_2.png"]];
[[mainTabBar.items objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:#"ic_3.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_3.png"]];
[[mainTabBar.items objectAtIndex:3] setFinishedSelectedImage:[UIImage imageNamed:#"ic_4.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_4.png"]];
[[mainTabBar.items objectAtIndex:4] setFinishedSelectedImage:[UIImage imageNamed:#"ic_5.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"ic_5.png"]];
This is code for tab bar. It is showing like -
Actual icon image is like (colors are like this)-
How can I show actual colors of icon?
You can use it like this if you are using iOS 8.
UIImage *image1 = [[UIImage imageNamed:#"message"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabBarItem1.image = image1;
tabBarItem1.selectedImage = image11;
Here is the source : UITabBarItem/selectedImage
By default, the actual selected image is automatically created from
the alpha values in the source image. To prevent system coloring,
provide images with UIImageRenderingModeAlwaysOriginal.
Set the UITabBarItems from inside their view controllers. Do not set them directly.
For example:
UITableViewController *searchTableCon = [[UITableViewController alloc]init];
UINavigationController *searchNavCon = [[UINavigationController alloc]initWithRootViewController:searchTableCon];
searchTableCon.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Search"
image:[UIImage imageNamed:#"Search"]
selectedImage:[UIImage imageNamed:#"Search"]];
You will have to do following thing for this.
UIImage * selectedImage = [UIImage imageNamed:#"selected_image.png"];
selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
viewCtrl.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Title" image:selectedImage selectedImage:selectedImage];
I had an application in which I am adding the images for the UITabBarItems programatically like this..
UIImage *selectedImage0 = [UIImage imageNamed:#"home_hvr.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"home.png"];
UIImage *selectedImage1 = [UIImage imageNamed:#"star_hvr.png"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"star.png"];
UITabBar *tabBar = self.tabbarcontroller.tabBar;
self.tabbarcontroller.tabBar.barStyle = UIBarStyleBlack;
self.tabbarcontroller.tabBar.translucent = NO;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
[item0 setSelectedImage:[selectedImage0 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item0 setImage:[unselectedImage0 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item1 setSelectedImage:[selectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[item1 setImage:[unselectedImage1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
since the tabbaritems doesn't have any titles it will bring a blank space at the bottom of the tab-bar pulling the image above the tab bar,I need no titles but needs to put my image inside the tab bar completely.
Can any body guide me on this?
UITabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
was done this trick worked for me .thanks
Try below code:
[[self.tabBarController.tabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:#"offer_btn_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"offer_btn.png"]];
[[self.tabBarController.tabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:#"merchant_btn_active.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"merchant_btn.png"]];
I made a custom tab bar with one of them extending beyond the bar. There is a line overlapping the center tab bar. Is there anyway I can get rid or this or hide it?
To do it, I just set tab bar images:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
tabBarItem1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
tabBarItem3.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:#"scheduleTabBarImageSel.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"scheduleTabBarImage.png"]];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:#"favoritesTabBarImageSel.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"favoritesTabBarImage.png"]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:#"searchTabBarImageSel.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"searchTabBarImage.png"]];
Any idea how to hide the line? Thanks
If I'm understanding your correctly, the problem is with the 1-2ish pixel shadow sitting on top of the tab bar. If this is the case, you can remove the shadow the same way you would with a navigation bar. Simply enable clips to bounds.
[self.tabBarController.tabBar setClipsToBounds:YES];
This seemed to work for me:
[[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
I am trying to create custom-tabs in my IOS app. I have followed this simple guide http://kurrytran.blogspot.se/2011/10/ios-5-tutorial-creating-custom-tab-bar.html Each tab should have 2 background pictures, one when being selected and one being unselected. My porblem is, in my real project I also have navigation controllers between my Tab bar controller and view controllers which seems to mess everything up. Simply nothing happens, all my tabs are black without any content in them. What shall I do to make it work ?
This is put in firstviewCotroller.m
- (void)viewDidLoad
{
UIImage *selectedImage0 = [UIImage imageNamed:#"HomeDB.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"HomeLB.png"];
UIImage *selectedImage1 = [UIImage imageNamed:#"ScheduleDB.png"];
UIImage *unselectedImage1 = [UIImage imageNamed:#"ScheduleLB.png"];
UIImage *selectedImage2 = [UIImage imageNamed:#"BuildingsDB.png"];
UIImage *unselectedImage2 = [UIImage imageNamed:#"BuildingsLB.png"];
UIImage *selectedImage3 = [UIImage imageNamed:#"InformationDB.png"];
UIImage *unselectedImage3 = [UIImage imageNamed:#"InformationLB.png"];
UIImage *selectedImage4 = [UIImage imageNamed:#"MoreDB.png"];
UIImage *unselectedImage4 = [UIImage imageNamed:#"MoreLB.png"];
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];
[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
[item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
[item4 setFinishedSelectedImage:selectedImage4 withFinishedUnselectedImage:unselectedImage4];
[super viewDidLoad];
}
Would it not be better to put the UINavigationControllers in each tab separately within your MainWindow.xib then in your App delegate use your above code to change the tab bar items in your App Delegate.
I'm doing the following in the "didFinishLaunchingWithOptions" method in my App Delegate.
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
[item0 setFinishedSelectedImage:[UIImage imageNamed:#"tab-1-selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-1.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:#"tab-2.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-2.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:#"tab-3-selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"tab-3.png"]];
Thanx for your answer AndyDev! This is my code below....
My AppleDelegate.h file:
#interface AppDelegate : UIResponder <UIApplicationDelegate>{
UITabBarController *tabBarController;
}
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
My AppleDelegate.m file:
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize tabBarController;
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
UITabBarItem *item4 = [tabBar.items objectAtIndex:4];
[item0 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
[item4 setFinishedSelectedImage:[UIImage imageNamed:#"BuildingsDB.png"] withFinishedUnselectedImage:[UIImage imageNamed:#"BuildingsLB.png"]];
return YES;
}
I am probably doing something wrong. I never tried to customized anything before in IOS and this is my first project I am developing in IOS. Do I need any code in each ViewController.m file aswell? Nothing happens atleast with this code....