need to set tabbar image in iphone? - iphone

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"];

Related

Creating TabBar programmatically only for one ViewController not in AppDelegate

I want to create a tabBar in my app but only in a detailView not in the AppDelegate. I am searching how to do this in google but everything what I have figured out is in AppDelegate.m
I am trying to do something like this. This shows me a Tab bar with two buttons(without names) but I want to go from one FirstViewController with tabBar, to one of the two items SecondViewController or ThirdViewController with a navigationBar with one backItemButton for get back to the FirstViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupTabBar];
}
- (void) setupTabBar {
tabBars = [[UITabBarController alloc] init];
NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
YPProfileViewController *profile = [[YPProfileViewController alloc] init];
YPProfileViewController *profile2 = [[YPProfileViewController alloc] init];
[localViewControllersArray addObject:profile];
[localViewControllersArray addObject:profile2];
tabBars.tabBarItem = profile2;
tabBars.viewControllers = localViewControllersArray;
tabBars.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);
[self.view addSubview:tabBars.view];
}
at least this works for me.
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = #"FIRST";
vc1.view.backgroundColor = [UIColor blueColor];
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = #"SECOND";
vc2.view.backgroundColor = [UIColor redColor];
UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = #[vc1,vc2];
tabBar.selectedIndex = 1;
tabBar.view.frame = CGRectMake(50, 50, 220, 320);
[tabBar willMoveToParentViewController:self];
[self.view addSubview:tabBar.view];
[self addChildViewController:tabBar];
[tabBar didMoveToParentViewController:self];

How to add UITabBarController programmatically (no xib file or storyboard)

I want to add a UITabBarController to my application. But I have to do it with code only. No xib files or storyboards. How to do this entirely through code?
EDIT:
_tbc = [[UITabBarController alloc] init];
aboutUsView = [[AboutUsView alloc] init];
helpView = [[HelpView alloc] init];
optionsView = [[OptionsView alloc] init];
self.navCon = [[UINavigationController alloc] initWithRootViewController:optionsView];
[self setnavigationCon:self.navCon];
[optionsView setdataLayer:self];
if ([navCon.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
UIImage *image = [UIImage imageNamed:#"Navigation Bar_reduced.png"];
[self.navCon.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
[optionsView addSelfView:window];
}
_tbc.viewControllers = [NSArray arrayWithObjects:navCon, aboutUsView, helpView, nil];
[window addSubview:_tbc.view];
Try this
AppDelegate.h
#interface AppDelegate : UIResponder <UITabBarControllerDelegate>
#property (strong, nonatomic) UITabBarController *tabBarController;
AppDeleGate.m
UINavigationController *nc1;
nc1 = [[UINavigationController alloc] init];
[nc1.navigationBar setTintColor:[UIColor blackColor]];
UIViewController *viewController1 = [[[FirstScreen alloc] initWithNibName:#"FirstScreen_ipad" bundle:nil] autorelease];
nc1.viewControllers = [NSArray arrayWithObjects:viewController1, nil];
UINavigationController *nc2;
nc2 = [[UINavigationController alloc] init];
[nc2.navigationBar setTintColor:[UIColor blackColor]];
UIViewController *viewController2 = [[[FullList alloc] initWithNibName:#"FullList_ipad" bundle:nil] autorelease];;
nc2.viewControllers = [NSArray arrayWithObjects:viewController2, nil];
UIViewController *viewController3 = [[[FavouriteView alloc] initWithNibName:#"FavouriteView_ipad" bundle:nil] autorelease];
UINavigationController *nc3;
nc3 = [[UINavigationController alloc] init];
[nc3.navigationBar setTintColor:[UIColor blackColor]];
nc3.viewControllers = [NSArray arrayWithObjects:viewController3, nil];
UIViewController *viewController4 = [[[UpcomingFights alloc] initWithNibName:#"UpcomingFights_ipad" bundle:nil] autorelease];
UINavigationController *nc4;
nc4 = [[UINavigationController alloc] init];
[nc4.navigationBar setTintColor:[UIColor blackColor]];
nc4.viewControllers = [NSArray arrayWithObjects:viewController4, nil];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:nc1, nc2,nc3,nc4 ,nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
ICON FOR TABBAR
In your ViewController.m file do as follow:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self.title = NSLocalizedString(#"YOUR View NAME", #"YOUR VIEW NAME");
self.tabBarItem.image = [UIImage imageNamed:#"YOUR IMAGE NAME"];
return self;
}
Add this code in your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
FirstViewController * fvc=[[FirstViewController alloc]initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController * svc=[[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
ThirdViewController * tvc=[[ThirdViewController alloc]initWithNibName:#"ThirdViewController" bundle:nil];
FourthViewController * fvc2=[[FourthViewController alloc]initWithNibName:#"FourthViewController" bundle:nil];
tabbar=[[UITabBarController alloc]init];
tabbar.viewControllers=[NSArray arrayWithObjects:fvc,svc,tvc,fvc2,nil];
[self.window addSubview:tabbar.view];
}
Here is the code from user1673099 in Swift (nice username btw... ;0) -
func initialize_tabs() {
var ncArr : [UINavigationController] = [UINavigationController]();
for _ in 0...3 {
let nc : UINavigationController = UINavigationController();
let vc = UIViewController();
nc.viewControllers = [vc];
let v : UIView = UIView(frame: UIScreen.mainScreen().bounds);
let redC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
let greenC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
let blueC : CGFloat = CGFloat(arc4random_uniform(255))/CGFloat(255);
v.backgroundColor = UIColor(red: redC, green: greenC, blue: blueC, alpha: 1);
let l : UILabel = UILabel(frame: UIScreen.mainScreen().bounds);
l.text = "Test Label";
l.textAlignment = .Center;
v.addSubview(l);
vc.view = v;
ncArr.append(nc);
}
self.tabBarController = UITabBarController();
self.tabBarController.viewControllers = ncArr;
return;
}

Not in AppDelegate create TabBarController

This is RegisterViewController.m part
if ([message isEqualToString:#"registerOK"]){
self.findViewController = [[FindViewController alloc] initWithNibName:nil bundle:NULL];
self.friendViewController = [[FriendViewController alloc] initWithNibName:nil bundle:NULL];
self.goViewController = [[GoViewController alloc] initWithNibName:nil bundle:NULL];
self.settingViewController = [[SettingViewController alloc] initWithNibName:nil bundle:NULL];
self.findNavigationController = [[UINavigationController alloc] initWithRootViewController:self.findViewController];
self.friendNavigationController = [[UINavigationController alloc] initWithRootViewController:self.friendViewController];
self.goNavigationController = [[UINavigationController alloc] initWithRootViewController:self.goViewController];
self.settingNavigationController = [[UINavigationController alloc] initWithRootViewController:self.settingViewController];
//[self.findNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav-bar-background-light.png"] forBarMetrics:UIBarMetricsDefault];
[self.findNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav-bar-background-light.png"] forBarMetrics:UIBarMetricsDefault];
[self.friendNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav-bar-background-light.png"] forBarMetrics:UIBarMetricsDefault];
[self.goNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav-bar-background-light.png"] forBarMetrics:UIBarMetricsDefault];
[self.settingNavigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav-bar-background-light.png"] forBarMetrics:UIBarMetricsDefault];
NSArray *allViewController = [[NSArray alloc] initWithObjects:self.findNavigationController,self.friendNavigationController,self.goNavigationController,self.settingNavigationController, nil];
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setViewControllers:allViewController];
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[self.view addSubview:self.tabBarController.view];
[window setRootViewController:self];
}
I want to create a TabBarController in RegisterViewController. How can I do it?
In AppDelegate.m I have an if which does: if already login then create TabBarController, if not go to RegisterViewController, then if register complete I want to create TabBarController. How to do this? Thanks.
You could just show your login and register views on top of tabBarController.view by [self.tabBarController presentViewController:] or such.
Make public method in AppDelegate and create there tabbar as usual.
And call this method where you need:
[(ASAppDelegate *)[[UIApplication sharedApplication] delegate]yourMethod]

Setting tabBarItem.badgeValue in other classes?

I seem to have a problem with my tabBar items. Doing the creation of the tabBar I'm able to set this property of the tabBarItem that I want like so:
viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIViewController *viewController3 = [[ThirdViewController alloc]initWithNibName:#"ThirdViewController" bundle:nil];
UIViewController *viewController4 = [[FourthViewController alloc]initWithNibName:#"FourthViewController" bundle:nil];
//Create our NavigationViewController object
NavigationViewController *navController = [[NavigationViewController alloc] initWithNibName:#"NavigationViewController" bundle:nil];
//Create our UINavigationController
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:navController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = #[nav, viewController2, viewController4, viewController3];
nav.tabBarItem = [[UITabBarItem alloc]
initWithTitle:(#"Menukort")
image:[UIImage imageNamed:#"162-receipt.png"]
tag:0];
viewController2.tabBarItem = [[UITabBarItem alloc]
initWithTitle:(#"Favoritter")
image:[UIImage imageNamed:#"28-star.png"]
tag:1];
viewController3.tabBarItem = [[UITabBarItem alloc]
initWithTitle:(#"Info")
image:[UIImage imageNamed:#"104-index-cards.png"]
tag:2];
viewController4.tabBarItem = [[UITabBarItem alloc]
initWithTitle:(#"Kort")
image:[UIImage imageNamed:#"103-map.png"]
tag:3];
viewController2.tabBarItem.badgeValue = #"1";
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
All this code is within a singleton, so sharing objects shouldn't be a problem. Now - how would I set the badgeValue of this tabBarItem in another class?
Thanks in advance.
Get array of tabbarController's viewController using
NSArray *arr = [self.tabbarController viewController];
Then set value for any view controller using
[[[arr objectIndex:1] tabBarItem] setbadgeValue:<nsinteger some value>];
Found the answer myself in another thread.
I did it like this:
[[[[[self tabBarController] tabBar] items]
objectAtIndex:tabIndex] setBadgeValue:badgeValueString];

ZUUIRevealController with UITabBarController instead of UINavigationController

I'm trying to implement the ZUUIRevealController into a project of mine, this app uses a UITabBarController with 3 tabs.
I went through the screencast and sample code multiple times, but I can't figure out why
[self.navigationController.parentViewController respondsToSelector:#selector(revealToggle)]
only responds with false.
In my appdelegate I just create a simple UITabBarController and add is as a rootViewController:
UITableViewController *activityViewController = [[[ActivityViewController alloc] initWithNibName:#"ActivityViewController" bundle:nil] autorelease];
UIViewController *agendaViewController = [[[AgendaViewController alloc] initWithNibName:#"AgendaViewController" bundle:nil] autorelease];
UIViewController *settingsViewController = [[[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil] autorelease];
UINavigationController *activityNavController = [[[UINavigationController alloc] initWithRootViewController:activityViewController] autorelease];
UINavigationController *agendaNavController = [[[UINavigationController alloc] initWithRootViewController:agendaViewController] autorelease];
UINavigationController *settingsNavController = [[[UINavigationController alloc] initWithRootViewController:settingsViewController] autorelease];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:activityNavController, agendaNavController, settingsNavController, nil];
SortViewController *sortViewController = [[SortViewController alloc] init];
ZUUIRevealController *revealController = [[ZUUIRevealController alloc] initWithFrontViewController:self.tabBarController rearViewController:sortViewController];
[sortViewController release];
//self.window.rootViewController = self.tabBarController;
self.window.rootViewController = revealController;
[revealController release];
[self.window makeKeyAndVisible];
But when I do the check in my ActivityViewController, I only get false
if([self.tabBarController.parentViewController respondsToSelector:#selector(revealToggle)])
{
NSLog(#"YAY");
}
else
{
NSLog(#"WRONG");
}
It seems so simple in the screencast, but it looks like I'm missing something.
ZUUIRevealController: https://github.com/pkluz/ZUUIRevealController
just put : in the if statement while you checking for respondsToSelector: like
if([self.tabBarController.parentViewController respondsToSelector:#selector(revealToggle:)])