My UITabBarItems aren't showing up vertically centered in my UITabBar. I am using a custom sized background image for my UITabBar.
UITabBarController *tabController = [[UITabBarController alloc] init];
UITabBar *tabBar = [controller tabBar];
UIImage *backgroundImage = [UIImage imageNamed:#"Images/bottomBar.png"];
[tabBar setBackgroundImage:backgroundImage];
SplashScreenController *introController = [[SplashScreenController alloc] init];
UIImage *introImage = [UIImage imageNamed:#"Images/navIntro.png"];
UITabBarItem *introItem = [[UITabBarItem alloc] initWithTitle:#"Intro" image:introImage tag:0];
[introController setTabBarItem:introItem];
NSMutableArray *controllers = [[NSMutableArray alloc] init];
[controllers addObject:introController];
NSArray *controllers = [self createControllers];
[tabController setViewControllers:controllers];
It looks like you are missing the retina version of your files, such as a 60x60 navIntro#2x.png. The navIntro#2x.png file needs to be in your project, but in code you would still reference the file as navIntro.png.
Related
I am unable to see NavigationBar on my TabView Controller.
I tried to add two tabs.
The class hierarchy is like below.
Please help.
ViewControllerOne : UIViewController
ViewControllerTwo : UIViewController
TabViewController : UITabBarController
ViewControllerOne *viewFirst = [[ViewControllerOne alloc]init];
ViewControllerOther *viewSecond = [[ViewControllerOther alloc]init];
[self.tabBarController.navigationController setViewControllers:#[viewFirst, viewSecond]];
[self.tabBarController.navigationController setTitle:#"TITLE TITLE"];
viewFirst.title=#"ONE";
viewFirst.tabBarItem.image = [UIImage imageNamed:#"first.png"];
viewSecond.title=#"TWO";
viewSecond.tabBarItem.image = [UIImage imageNamed:#"second.png"];
viewFirst.view.backgroundColor = [UIColor grayColor];
viewSecond.view.backgroundColor = [UIColor whiteColor];
self.viewControllers = [NSArray arrayWithObjects:viewFirst, viewSecond, nil];
I'm not sure what "self" is, but it looks like it is just a random view controller. More context on that would be good, but I'll try to throw some thoughts.
It looks like you're trying to set viewFirst and viewSecond as a navigation stack, rather then the two different tabs. It also appears like you aren't placing those controllers within UINavigationControllers. Try something more along these lines:
ViewControllerOne *one = [[ViewControllerOne alloc] init];
one.title = #"ONE";
one.tabBarItem.image = [UIImage imageNamed:#"first.png"];
one.view.backgroundColor = [UIColor grayColor];
UINavigationController *navOne = [[UINavigationController alloc] initWithRootViewController:one];
ViewControllerTwo *two = [[ViewControllerOne alloc] init];
two.title = #"SECOND";
two.tabBarItem.image = [UIImage imageNamed:#"second.png"];
two.view.backgroundColor = [UIColor whiteColor];
UINavigationController *navTwo = [[UINavigationController alloc] initWithRootViewController:two];
TabViewController *tabController = [[TabViewController alloc] init];
[tabController setViewControllers:#[navOne, navTwo] animated:NO];
Make sure that the tabController is the key UIWindow's root view controller.
i have selected tab bar based application from templates. in that i need to set the custom image i have tried like given below
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UINavigationController *view1=[[UINavigationController alloc]initWithRootViewController:viewController1];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
UINavigationController *view2=[[UINavigationController alloc]initWithRootViewController:viewController2];
UIViewController *viewController3 = [[[SecondViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil] autorelease];
UINavigationController *view3=[[UINavigationController alloc]initWithRootViewController:viewController3];
UIViewController *viewController4 = [[[SecondViewController alloc] initWithNibName:#"MainViewController" bundle:nil] autorelease];
UINavigationController *view4=[[UINavigationController alloc]initWithRootViewController:viewController4];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.delegate=self;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:view1,view2,view3,view4, nil];
[self.tabBarController.tabBarItem setImage:[UIImage imageNamed:#"h.png"]];
one more way i have tried
[[_tabBarController tabBar] setBackgroundImage:[UIImage imageNamed:#"h.png"]];
in the second way there is image applied but it's a background.
so i need individually like home button something like that....
how to set this...
thanks....
You an set the images in UIViewControllers's TabBarItems like..
viewcontroller1.tabBarItems.image = [UIImage imageNamed:#"your image1"];
viewController2.tabBarItems.image = [UIImage imageNamed:#"your image2"];
viewController3.tabBarItems.image = [UIImage imageNamed:#"your image3"];
viewController3.tabBarItems.image = [UIImage imageNamed:#"your image3"];
You can do like,
NSArray *tabs = tabBarController.viewControllers;
UIViewController *tab1 = [tabs objectAtIndex:0];
tab1.tabBarItem.image = [UIImage imageNamed:#"clockicon.png"];
UIViewController *tab2 = [tabs objectAtIndex:1];
tab2.tabBarItem.image = [UIImage imageNamed:#"nearest.png"];
In a UITabBarController, upon selecting a tab, I want that tab's UIViewController to change (assign a new viewcontroller). I'm trying this-
NSMutableArray *tabBarViewControllers = [myUITabBarController.viewControllers mutableCopy];
[tabbarViewControllers replaceObjectAtIndex:0 withObject:[[myViewcontroller1 alloc] init]];
[myUITabBarController setViewControllers:tabbarViewControllers];
But it gives error. How to assign a new UIViewController and refresh instantly?
This is based on femina's answer but doesn't require you to build the whole array of view controllers, it just lets you replace an existing view controller with another. In my case I wanted to swap in a different xib file for the iPhone 5 screen:
if ([[UIScreen mainScreen] bounds].size.height == 568) {
NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
Tracking *tracking568h = [[Tracking alloc] initWithNibName:#"Tracking-568h" bundle:nil];
tracking568h.title = [[viewControllers objectAtIndex:0] title];
tracking568h.tabBarItem = [[viewControllers objectAtIndex:0] tabBarItem];
[viewControllers replaceObjectAtIndex:0 withObject:tracking568h];
[tracking568h release];
[self.tabBarController setViewControllers:viewControllers animated:FALSE];
}
This changes the view controller for the first tab, keeping the same tab icon and label.
Please see this code , it offers 2 tabbar's with navigation.
In the AppDelegate.h please declare
UINavigationController *nav1;
UINavigationController *nav2;
UITabBarController *tab;
And in the Appdelegate.m , in the didFinishLaunchingWithOptions please add:-
tab = [[UITabBarController alloc]init];
ViewController *view1 = [[ViewController alloc]init];
nav1= [[UINavigationController alloc]initWithRootViewController:view1];
UITabBarItem *tab1 = [[UITabBarItem alloc]initWithTitle:#"Add" image:[UIImage imageNamed:#"Plus.png"] tag:1];
view1.title = #"Add";
[view1 setTabBarItem:tab1];
SettingsViewController *view2 = [[SettingsViewController alloc]init];
nav2= [[UINavigationController alloc]initWithRootViewController:view2];
UITabBarItem *tab2 = [[UITabBarItem alloc]initWithTitle:#"Setting" image:[UIImage imageNamed:#"settings.png"] tag:2];
view2.title = #"Setting";
[view2 setTabBarItem:tab2];
tab.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nil];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = tab;
Also check this link for further implementation ... Hope this helps :)
UItabBar changing View Controllers
In the AppDelegate.h
UIViewController *vc1;
UIViewController *vc2;
UIViewController *vc3;
in the Appdelegate.m
in the didFinishLaunchingWithOptions
NSMutableArray *listOfViewControllers = [[NSMutableArray alloc] init];
vc1 = [[UIViewController alloc] init];
vc1.title = #"A";
[listOfViewControllers addObject:vc1];
vc2 = [[UIViewController alloc] init];
vc2.title = #"B";
[listOfViewControllers addObject:vc2];
vc3 = [[UIViewController alloc] init];
vc3.title = #"C";
[listOfViewControllers addObject:vc3];
[self.tabBarController setViewControllers:listOfViewControllers
animated:YES];
I am trying to achieve the below attach image on the UITabbar.
http://i.imgur.com/7Tukx.png
It's pretty straight forward, on selection the tab expand with a label to display text and on selection on another tab, the selected tab will collapse.
I am not really sure how to do this with the UItabbar.
I will appreciate if anyone could point me in the right direction.
for the custom tab you use this method on appdelegate class
this method create two tab bar item.
and you set your image..
-(void)SetTabs
{
tabBarController = [[UITabBarController alloc]init];
NSMutableArray *localControllerArray = [[NSMutableArray alloc]initWithCapacity:2];
UIImage *image = [UIImage imageNamed:#"home.png"];
GreetingCardsViewController *GreetingCardsView = [[[GreetingCardsViewController alloc] initWithNibName:#"GreetingCardsViewController" bundle:nil] autorelease];
UITabBarItem *item = [[UITabBarItem alloc]initWithTitle:#"Home" image:image tag:0];
UINavigationController *GreetingCardsViewNavigationController = [[UINavigationController alloc]initWithRootViewController:GreetingCardsView];
GreetingCardsView.tabBarItem = item;
[localControllerArray addObject:GreetingCardsViewNavigationController];
UIImage *image1 = [UIImage imageNamed:#"heart1.png"];
Cards *CardView = [[[Cards alloc] initWithNibName:#"Cards" bundle:nil] autorelease];
UITabBarItem *item1 = [[UITabBarItem alloc]initWithTitle:#"Cards" image:image1 tag:1];
UINavigationController *CardNavigationController = [[UINavigationController alloc]initWithRootViewController:CardView];
CardNavigationController.tabBarItem = item1;
[localControllerArray addObject:CardNavigationController];
[tabBarController setViewControllers:localControllerArray];
[_window addSubview:tabBarController.view];
[localControllerArray release];
}
I'm not entirely sure what you're after, however I think this may be relevant to you. You could use the basic concept and tweak it to what you need.
So my app has a UITabBarController. Within that, one of the tabs is a UIViewController containing a UINavigationController. Within that is a UITableViewController.
The problem is that for some reason the size of the TableView is off, and part of the bottom entry of the table is obscured by the TabBar. I've tried everything I can think of to resize the tableview but to no avail. Here are the relevant parts of the code:
AppDelegate
tabBarController = [[UITabBarController alloc] init];
LogbookViewController *firstVC = [[LogbookViewController alloc] init];
PaceCalcViewController *secondVC = [[PaceCalcViewController alloc] init];
SettingsTableViewController *thirdVC = [[SettingsTableViewController alloc] init];
...
// Add them as children of the tab bar controller
tabBarController.viewControllers = [NSArray arrayWithObjects: firstVC, secondVC, thirdVC, nil];
LogbookViewController (subclass of UIViewController)
- (void)viewDidLoad {
[super viewDidLoad];
navigationController = [[UINavigationController alloc] init];
[self.view addSubview:navigationController.view];
WorkoutListTableViewController *listController = [[WorkoutListTableViewController alloc] init];
listController.managedObjectContext = self.managedObjectContext;
[navigationController pushViewController:listController animated:NO];
[listController release];
}
The WorkoutListTableViewController (subclass of UITableViewcontroller) currently doesn't have anything in particular in it that I would think would affect it, but here's the ViewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
self.title = #"Workouts";
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"List" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *fileName = #"short_bg.png";
NSString *filePath = [paths stringByAppendingPathComponent:fileName];
UIImage *paper = [[UIImage alloc] initWithContentsOfFile:filePath];
UIImageView *paper_bg = [[UIImageView alloc] initWithImage:paper];
self.tableView.backgroundView = paper_bg;
[fileName release];
[paper release];
//self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add:)];
self.navigationItem.rightBarButtonItem = addButtonItem;
[addButtonItem release];
/*UIBarButtonItem *importButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Import" style:UIBarButtonItemStylePlain target:self action:#selector(importDialogAction:)];
self.navigationItem.leftBarButtonItem = importButtonItem;
[importButtonItem release];*/
}
}
I've tried things like listController.view.frame = CGRectMake(whatever) in the LogbookViewcontroller, or self.tableView.frame - CGRectMake(whatever) in the WorkoutListTableViewController, but nothing has worked. Any ideas? I can post more of the code if that helps.
You should be added the Navigation Controller directly to the UITabBarController, then it should size appropriately:
tabBarController = [[UITabBarController alloc] init];
WorkoutListTableViewController *listController = [[WorkoutListTableViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:listController];
PaceCalcViewController *secondVC = [[PaceCalcViewController alloc] init];
SettingsTableViewController *thirdVC = [[SettingsTableViewController alloc] init];
...
// Add them as children of the tab bar controller
tabBarController.viewControllers = [NSArray arrayWithObjects: navController, secondVC, thirdVC, nil];
You don't need the View Controller in the 1st tab to add a UINavigationController to the view, just go ahead and add the TableViewController as the root of the nav controller and add the nav controller into the 1st tab.