ios Tabbar selected index set image on didSelectViewController - ios

I am Creating One Tab-bar by programmatically , Tab-bar working Properly the Problem is I don't know How to set selected index , selected tab-bar set blue color , set blue image
Like How to know this tab-bar item is selected , didSelectViewController delegate method is used but i don't understand how set image
This is method using but i dont know how to set image and color
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
Here is my code
-(IBAction)clicka:(id)sender
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
AViewController *viewController1 = [[AViewController alloc] initWithNibName:#"AViewController" bundle:nil];
BViewController *viewController2 = [[BViewController alloc] initWithNibName:#"BViewController" bundle:nil];
CViewController *viewController3 = [[CViewController alloc]initWithNibName:#"CViewController" bundle:nil];
DViewController *viewController4 = [[DViewController alloc]initWithNibName:#"DViewController" bundle:nil];
[self.navigationController pushViewController:viewController1 animated:YES];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.delegate=self;
self.tabBarController.viewControllers = #[viewController1,viewController2,viewController3,viewController4];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:self.tabBarController];
UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320,100)];
img.image=[UIImage imageNamed:#"yellow-bg.png"];
[self.tabBarController.tabBar addSubview:img];
UIImageView *imghome=[[UIImageView alloc]initWithFrame:CGRectMake(30.0,5,25,25)];
imghome.image=[UIImage imageNamed:#"splash-logo.png"];
[img addSubview:imghome];
UIImageView *imghome1=[[UIImageView alloc]initWithFrame:CGRectMake(100.0,5,25,25)];
imghome1.image=[UIImage imageNamed:#"chat-icon.png"];
[img addSubview:imghome1];
UIImageView *imghome2=[[UIImageView alloc]initWithFrame:CGRectMake(180.0,5,25,25)];
imghome2.image=[UIImage imageNamed:#"p-icon.png"];
[img addSubview:imghome2];
UIImageView *imghome3=[[UIImageView alloc]initWithFrame:CGRectMake(260.0,5,25,25)];
imghome3.image=[UIImage imageNamed:#"addddd.png"];
[img addSubview:imghome3];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSUInteger selectedIndex = self.tabBarController.selectedIndex;
NSLog(#"%lu",(unsigned long)selectedIndex);
}
please Help me

You can find out the render mode in UIImage.h
- (UIImage *)imageWithRenderingMode:(UIImageRenderingMode)renderingMode
By the default , if the image was attached to a tabbar as tabbarItem, the rednering mode is UIImageRenderingModeAlwaysTemplate, so it's is blue.
You should change the image rendering mode before you set to the tabbaritem.
UIImage * originalImage = [UIImage imageNamed:#"chat-icon.png"];
UIImage * selectedBlueImage = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
when the tab was selected, set the selectedBlueImage to the imageView. like : imageview.highlightedImage = selectedBlueImage

I am not sure if I understood you problem correctly, but if you want to set images inside each TabBar section for each View Controllers correspondingly, you can do that from each View Controller class, for example, inside AViewController.m, override:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//self.tabBarItem.title = #"Title";
self.tabBarItem.image = [UIImage imageNamed:#"image-name"];
}
return self;
}

Related

IOS. UITabBarController with navigationController

I had a viewController which contains video view and UITabBarController. UITabBarController contains 3 viewControllers with UINavigationController. The problem is that the frame of first UINavigationController is good (0,20,width,height), but the other two navigationController has wrong frame (0,0,width,height). Status bar is showing, so I don't know what's wrong with them.
Here i create TabBarController :
#interface EEMenuTBC ()
#end
#implementation EEMenuTBC
- (void)viewDidLoad {
[super viewDidLoad];
EEFavoritesVC *favotiteVC = [[EEFavoritesVC alloc] initWithNibAsClassName];
UINavigationController *favotiteNC = [[UINavigationController alloc] initWithRootViewController:favotiteVC];
[favotiteVC.tabBarItem setImage:[UIImage imageNamed:#"favoritesMenuDisabled"]];
[favotiteVC.tabBarItem setTitle:#"Favourites"];
EESearchTVC *searchVC = [[EESearchTVC alloc] initWithNibAsClassName];
UINavigationController *searchNC = [[UINavigationController alloc] initWithRootViewController:searchVC];
[searchVC.tabBarItem setImage:[UIImage imageNamed:#"searchMenuDisabled"]];
[searchVC.tabBarItem setTitle:#"Search"];
EESettingsTVC *settingsVC = [[EESettingsTVC alloc] initWithNibAsClassName];
UINavigationController *settingsNC = [[UINavigationController alloc] initWithRootViewController:settingsVC];
[settingsVC.tabBarItem setImage:[UIImage imageNamed:#"settingsMenuDisabled"]];
[settingsVC.tabBarItem setTitle:#"Settings"];
self.viewControllers = #[favotiteNC, searchNC, settingsNC];
self.tabBar.items[0].selectedImage = [UIImage imageNamed: #"favoritesMenuEnabled"];
self.tabBar.items[1].selectedImage = [UIImage imageNamed: #"searchMenuEnabled"];
self.tabBar.items[2].selectedImage = [UIImage imageNamed: #"settingsMenuEnabled"];
for(UIView *temp in self.tabBar.subviews) {
[temp setExclusiveTouch:YES];
}
}
-(BOOL)prefersStatusBarHidden {
return NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)goToRootVC {
if (_delegateMenuTBC && [_delegateMenuTBC respondsToSelector:#selector(EEMenuTBCDelegateGoToRootVC)]) {
[_delegateMenuTBC EEMenuTBCDelegateGoToRootVC];
}
}
#end
In other ViewController i doing
_menuTBC = [[EEMenuTBC alloc] initWithNibName:#"EEMenuTBC" bundle:nil];
_menuTBC.view.frame = self.view.frame;
And in favotiteVC navigation look's good, and all other's 2 navigations has no spacing from status bar
P.S
First of all, I added my UITabBarController as a child to UIViewController. The first UINavigationController from UITabBarController has right frame (0,20,width,height) , but others 2 UINavigationsControllers, has wrong frame (0,0,width, height). I think they don't have status bar, and I checked that method -prefersStatusBarHidden isn't call from all UIViewControllers which contains in UITabBarController

How to make a tab bar application out of standalone NIBs?

I have 5 standalone table view controller nibs (with custom cell implementation) accessible through an another table view menu list (no storyboards)
The client desires to have all 5 nibs in tabs. So I need to get rid of the menu list and provide nibs in TABs .
how can I do this ?
First add this property to your AppDelegate.h
#property (strong, nonatomic) UITabBarController *tabBarController;
make a method to set the views and set up your tabbar like:
-(void)setViews
{
UIViewController *viewController1 = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] ;
UIViewController *viewController2 = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UIViewController *viewController3 = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
UIViewController *viewController4 = [[FourthViewController alloc] initWithNibName:#"FourthViewController" bundle:nil];
UIViewController *viewController5 = [[FifthViewController alloc] initWithNibName:#"FifthViewController" bundle:nil];
UINavigationController *navigationController1=[[UINavigationController alloc]initWithRootViewController:viewController1];
[navigationController1.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController2=[[UINavigationController alloc]initWithRootViewController:viewController2];
[navigationController2.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController3=[[UINavigationController alloc]initWithRootViewController:viewController3];
[navigationController3.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController4=[[UINavigationController alloc]initWithRootViewController:viewController4];
[navigationController4.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
UINavigationController *navigationController5=[[UINavigationController alloc]initWithRootViewController:viewController5];
[navigationController5.navigationBar setBackgroundImage:[UIImage imageNamed:#"upwhitebg.png"] forBarMetrics:UIBarMetricsDefault];
[navigationController1.navigationBar setHidden:YES];
[navigationController2.navigationBar setHidden:YES];
[navigationController3.navigationBar setHidden:YES];
[navigationController4.navigationBar setHidden:YES];
[navigationController5.navigationBar setHidden:YES];
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController.tabBar setBackgroundColor:[UIColor clearColor]];
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar"];
[[[self tabBarController]tabBar]setSelectionIndicatorImage:[UIImage imageNamed:#"transparent.png"]];
[self.tabBarController setDelegate:self];
self.tabBarController.viewControllers = #[navigationController1, navigationController2,navigationController3,navigationController4,navigationController5];
self.window.rootViewController = self.tabBarController;
}
avoid the set images and setHidden if you don't want to or not want to make the custom navigation bar.
and call this method in your didFinishLaunchingWithOptions.
Now set up the delegate method for tabbar and you can set the custom images over there:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if (tabBarController.selectedIndex == 0)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-1"];
}
else if (tabBarController.selectedIndex == 1)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-2"];
}
else if (tabBarController.selectedIndex == 2)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-3"];
}
else if (tabBarController.selectedIndex == 3)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-4"];
}
else if (tabBarController.selectedIndex == 4)
{
self.tabBarController.tabBar.backgroundImage = [UIImage imageNamed:#"tabbar-5"];
}
}
I'd set it all up in the main nib/storyboard, but it's easier to show in code. You can create the view controllers in the usual way (again, nib or storyboard or code).
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:#[vc1, vc2, vc3, vc4, vc5] animated:YES];
UIWindow *window = [[UIApplication sharedApplication] delegate].window;
[window setRootViewController:tabBarController];

Show Multiple views using custom tabs within UiViewController

I'm wondering how I would be able to design my own custom tabs, that when clicked could show mutiple view on the screen
http://tinypic.com/r/2cxtjk7/5
I'm only currently able to find the bottom tabbed bar approach. Thanks for your help im quite new still so if you could explain in detail that would be great :)
Show Multiple views using custom tabs
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
AccountViewController *accountViewController = [[AccountViewController alloc]
initWithNibName:#"AccountViewController" bundle:nil];
MoreViewController *moreViewController = [[MoreViewController alloc]
initWithNibName:#"MoreViewController" bundle:nil];
BarTabViewController *barTabViewController = [[BarTabViewController alloc]
initWithNibName:#"BarTabViewController" bundle:nil];
LocationsViewController *locationsViewController = [[LocationsViewController alloc]
initWithNibName:#"LocationsViewController" bundle:nil];
self.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
barTabViewController, moreViewController, nil];
[self.view addSubview:locationsViewController.view];
self.selectedController = locationsViewController;
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.selectedIndex = 0;
self.tabBarController.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController,
barTabViewController, moreViewController, nil];
self.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController pushViewController:delegate.tabBarController animated:YES];
return self;
}
Like I said, this will display the selected controller properly, however when the app launches and I try to switch views with the tab bar, the subview just become grey... The following is the code to switch items:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (item == locationsTabBarItem) {
UIViewController *locationsController = [viewControllers objectAtIndex:0];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:locationsController.view];
self.selectedController = locationsController;
}
else if (item == accountsTabBarItem) {
UIViewController *accountsController = [viewControllers objectAtIndex:1];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:accountsController.view];
self.selectedController = accountsController;
}
else if (item == barTabTabBarItem) {
UIViewController *barTabController = [viewControllers objectAtIndex:2];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:barTabController.view];
self.selectedController = barTabController;
}
else {
UIViewController *moreController = [viewControllers objectAtIndex:3];
[self.selectedController.view removeFromSuperview];
[self.view addSubview:moreController.view];
self.selectedController = moreController;
}
}
I added thing in view did load in the end here:
PatientDetailsViewController *patientDetailsView =
[[PatientDetailsViewController alloc]
initWithNibName:#"PatientDetailsViewController" bundle:nil];
[self addChildViewController:patientDetailsView];
[self.patientDetailsView addSubview:patientDetailsView.view];
[self.patientDetailsView setClipsToBounds:YES];
and then animated its opening with a button by adjusted the view frame size with an animation.

use storyboard with custom UITabbarController

i have been working on this issue for some time now and cannot find a solution to my problem.
i have a tabbar view controller that i have tried to customise with images, i have the custom graphics working however i need to use code to display and init the tabbar's view controllers. i also have a problem with displaying a navigation bar at the top of one of my tabs which i think is connected to how i am initiating the tab view controllers
the storyboard shows that there should be a navigation bar at the top of the medication tab and that the view is connected to the tab bar via a segue
you can see i have tried to use storyboard segues to link my view controllers to the tab bar controller. i have the following code in the MedicationViewController.m
/
// MedicationViewController.m
// fibromapp
//
// Created by jamie mcallister on 08/09/2013.
// Copyright (c) 2013 Jamie McAllister. All rights reserved.
//
#import "MedicationViewController.h"
#import "TakenViewController.h"
#import "MedsListViewController.h"
#import "MedsAlarmViewController.h"
#interface MedicationViewController ()
#end
#implementation MedicationViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
TakenViewController *viewController2 = [[TakenViewController alloc] init];
MedsListViewController *viewController1 = [[MedsListViewController alloc] init];
MedsAlarmViewController *viewController3 = [[MedsAlarmViewController alloc] init];
self.viewControllers = [NSArray arrayWithObjects:viewController1,
viewController2,
viewController3,nil];
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:#"Medication" image:[UIImage imageNamed:NULL] tag:1];
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:#"Taken" image:[UIImage imageNamed:NULL] tag:2];
UITabBarItem *tab3 = [[UITabBarItem alloc] initWithTitle:#"Alarms" image:[UIImage imageNamed:NULL] tag:3];
UIImage* sel = [UIImage imageNamed:#"fmtabSel"];
[viewController1 setTabBarItem:tab1];
[viewController2 setTabBarItem:tab2];
[viewController3 setTabBarItem:tab3];
UIImage* tabBarBackground = [UIImage imageNamed:#"fmtab.png"];
UITabBar *tabBar = self.tabBar;
[tabBar setBackgroundImage:tabBarBackground];
[tabBar setSelectionIndicatorImage:sel];
}
return self;
}
- (void)viewDidLoad
{
UITabBar *tabbar = self.tabBar;
NSLog(#"%f %f", tabbar.frame.size.width, tabbar.frame.size.height);//used to find the size of the bar
[super viewDidLoad];
UIImage* tabBarBackground = [UIImage imageNamed:#"fmtab.png"];
UIImage* sel = [UIImage imageNamed:#"fmtabSel"];
UITabBar *tabBar = self.tabBar;
[tabBar setBackgroundImage:tabBarBackground];
[tabBar setSelectionIndicatorImage:sel];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
with this code i get the tab but there is no navigation bar at the top of this tab.
can anybody suggest what i must do to resolve this?
if you require any more information feel free to ask and i will edit it into the bottom of this question.
thanks in advance :)
To have a Navigation bar, you have to put a UINavigationController between the tabbar controller and the first UIViewController.
All can be done in storyboard without needs of writing a line of code.
If you want the navigation bar to be at the top You should fill your tabbar controller with navigation controllers inited with root controllers, not just plain controllers.
Smth like that:
TakenViewController *viewController2 = [[TakenViewController alloc] init];
MedsListViewController *viewController1 = [[MedsListViewController alloc] init];
MedsAlarmViewController *viewController3 = [[MedsAlarmViewController alloc] init];
UINavigationController * nc1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController * nc2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController * nc3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
self.viewControllers = [NSArray arrayWithObjects:nc1,
nc2,
nc3,nil];

iOS - Second View is not displaying over tab view

I have a tabBarView and a GWDNativeViewController. GWDNativeViewController is a main menu. I am trying to get that view to display first and overtop the tabBarView. I have this same code set within an IBAction and it works. When I put it into the Loaddidfinishwithoptions it doesn't work.
#implementation GWDNativeAppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
#synthesize secondView = _secondView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[GWDNativeFirstViewController alloc] initWithNibName:#"GWDNativeFirstViewController" bundle:nil];
UIViewController *viewController2 = [[GWDNativeSecondViewController alloc] initWithNibName:#"GWDNativeSecondViewController" bundle:nil];
UIViewController *viewController3 = [[GWDNativeThirdViewController alloc] initWithNibName:#"GWDNativeThirdViewController" bundle:nil];
UIViewController *viewController4 = [[GWDNativeFourthViewController alloc] initWithNibName:#"GWDNativeFourthViewController" bundle:nil];
UIViewController *viewController5 = [[GWDNativeFifthViewController alloc] initWithNibName:#"GWDNativeFifthViewController" bundle:nil];
//tabBarController Stuff
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, viewController4, viewController5, nil];
self.window.rootViewController = self.tabBarController;
//Specify W`enter code here`hich tab to display (Number need to be set based on button selected)
//self.tabBarController.selectedIndex = 2;
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.tabBarController presentModalViewController:secondView animated:YES];
[self.window makeKeyAndVisible];
return YES;
}
Here is the the code in the GWDNativeFirstViewController.m
The code in the IBAction works.. Code in the viewDidLoad doesn't.?
-(IBAction)pressedButton {
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:secondView animated:YES];
//[secondView release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"First", #"First");
self.tabBarItem.image = [UIImage imageNamed:#"first"];
}
return self;
}
- (void)viewDidLoad
{
[contentWebView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://m.web.org/?iapp=1#tab1"]]];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.tabBarController presentModalViewController:secondView animated:YES];
}
I'm not sure why it doesn't work in viewDidLoad, but it can work in applicationDidFinishLaunchingWithOptions: if you put your [self.window makeKeyAndVisible] line before you define secondView.
[self.window makeKeyAndVisible];
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.tabBarController presentModalViewController:secondView animated:YES];
If you want it to appear immediately, change the animated option to NO, and delete the line defining the modalTransitionStyle.
The tabBarController's view hasn't loaded here yet. You could put that code into the viewDidLoad in one of your 5 view controllers.

Resources