placing a Tabbar controller on top of viewcontroller - ios

How do i place the TABBAR control on top of view control.by default it is at bottom how do i put it up..?
Used following code to do:
UIImage* tabBarBackground = [UIImage imageNamed:#"footer-bg.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
[[UITabBar appearance]setSelectionIndicatorImage:[UIImage imageNamed:#"footer-hover-bg.png"]];
if(IS_IOS_7)
{
[[appDelegate.tabBarController tabBar] setSelectionIndicatorImage:[UIImage imageNamed:#"footer-hover-bg.png"]];
[[UITabBar appearance] setTintColor:[UIColor blackColor]];
}
appDelegate.tabBarController.tabBar.frame = CGRectMake(0, 20, 320, 50);
appDelegate.tabBarController.delegate=self;
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, -20, 320, 600)];
viewControllers = [[NSMutableArray alloc] init];
MainViewController *view1 = [[MainViewController alloc] init];
UINavigationController *nav1=[[UINavigationController alloc]initWithRootViewController:view1];
[viewControllers addObject:nav1];
ViewController1 *view2 = [[ViewController1 alloc] init];
UINavigationController *nav2=[[UINavigationController alloc]initWithRootViewController:view2];
[viewControllers addObject:nav2];
[appDelegate.tabBarController setViewControllers:viewControllers];
appDelegate.tabBarController.tabBarController.view.frame=CGRectMake(0, 0, 320, 480);
[view addSubview:appDelegate.tabBarController.view];
[self.view addSubview:view];
UITabBarItem *tabBarItem1 = [appDelegate.tabBarController.tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [appDelegate.tabBarController.tabBar.items objectAtIndex:1];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:[arraySelectedImages objectAtIndex:0]] withFinishedUnselectedImage:[UIImage imageNamed:[arrayUnselectedImages objectAtIndex:0]]];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:[arraySelectedImages objectAtIndex:1]] withFinishedUnselectedImage:[UIImage imageNamed:[arrayUnselectedImages objectAtIndex:1]]];
How do i do the same using storyboards?

First add a UIViewController then add the UITabBar and add contraint if you use autolayout.
Don't forgot to connect delegate.
Hope that will help.

Related

iOS: TabBar controller above UIView

I am having a problem with my tab bar placed above my view
I am using iOS 8 and Autolayout
I have my main view which has a button that opens another view that has the tab bar:
-(IBAction)btnOpen:(id)sender
{
Home* homevc = [[Home alloc] initWithNibName:#"Home_iPhone" bundle:nil];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:homevc];
[nc.tabBarItem setTitle:#"Home"];
[nc.tabBarItem setImage:[UIImage imageNamed:#"home_icon.png"]];
Search *searchvc = [[Search alloc] initWithNibName:#"Search_iPhone" bundle:nil];
[searchvc.tabBarItem setTitle:#"Search"];
[searchvc.tabBarItem setImage:[UIImage imageNamed:#"search_icon.png"]];
UITabBarController *tabController = [[UITabBarController alloc] init];
[tabController setViewControllers:[NSArray arrayWithObjects:nc,searchvc, nil]];
UIColor * color = [UIColor colorWithRed:191/255.0f green:50/255.0f blue:46/255.0f alpha:1.0f];
[[UITabBar appearance] setTintColor:color];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:color, NSForegroundColorAttributeName, nil]
forState:UIControlStateSelected];
[self presentViewController:tabController animated:YES completion:nil];
}
How can I place the Home view above the tab bar?

Changing UINavigationBar later in View Controller

I have a view controller that does not have a NavigationController (and can't). I define a navigation bar like this.
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 41)];
navBar.backgroundColor = UIColorFromRGB(0x72CE97);
navBar.barTintColor = UIColorFromRGB(0x72CE97);
//Keep the container for Navigation Items
UINavigationItem *navItem = [[UINavigationItem alloc] init];
UIImage* logoImage = [UIImage imageNamed:#"nav_logo_small.png"];
navItem.titleView = [[UIImageView alloc] initWithImage:logoImage];
//Add the left button as an Item to Navigation Items
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButton:)];
navItem.leftBarButtonItem = leftButton;
//Add Navigation Items Container to Navigation Bar
navBar.items = #[ navItem ];
//Display the Navigation Bar
[self.view addSubview:navBar];
[navBar release];
How do I change the background later in the View Controller?
Below doesn't work. I think it is because I don't have a NavigationController:
NavigationBar *navigationBar = self.navigationController.navigationBar;
navigationBar.backgroundColor = [UIColor colorWithRed:197.0f/255.0f green:174.0f/255.0f blue:135.0f/255.0f alpha:1.0];
navigationBar.barTintColor = [UIColor colorWithRed:197.0f/255.0f green:174.0f/255.0f blue:135.0f/255.0f alpha:1.0];
Try this:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 41)];
navBar.tag = 1;
...
NavigationBar *navigationBar = (NavigationBar *)[self.view viewWithTag:1];

how to add UITabBarItem programmatically?

In my application, i have added UITabBarController in "Storyborad".
But now i want to add UITabBarItem programmatically. Let's say there are 5 buttons and when ever user click on that button my UITabBarController is called.
There are total 5 tab in "tabbar".
e.g:
Tab name:item1,item2,item3,item4,item5.
Button name:item1,item2,item3,item4,item5.
Let's say user click on item1 button, UITabBarcontroller is called. Now user should not be able to see that item1 tab in "tabbarcontroller", he should be able to see the reset "tabBaritem".
Can anyone help me out with this?
How to do it programmatically?
Thanks.
Nilesh J
UITabBarItem * itemNew = [[UITabBarItem alloc] initWithTitle:#"Page 1"
image:[UIImage imageNamed:#"page1_image_normal"]
selectedImage:[UIImage imageNamed:#"page1_image_selected"]];
Get existing tabBarItems
NSMutableArray *tbItems = [NSMutableArray arrayWithArray:[self.tabBar items]];
//Add your new tabBarItem
[tbItems addObject:itemNew];
//Set your tabBar items to the new array
[self.tabBar setItems:tbItems];
Swift 4
if var controllers = tabBarController?.viewControllers {
let tabItem = UITabBarItem(title: "Item \(controllers.count)", image: nil, selectedImage: nil)
let vc = UIViewController() // your new view controller
vc.tabBarItem = tabItem
controllers.append(vc) // or insert at index up to you
tabBarController?.setViewControllers(controllers, animated: true)
}
To call tabBar click event you need to do the following:
call delegatemethod on UITabBarDelegate and implement their method:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
//self.tabBarItem.title = #"Title";
}
This is for Custom Tabbar Controller. In this code you can give your Tabbar Button Image and when you click one Button that buttom will be changed according to your image.
Set Delegate Method for UITabBarControllerDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self creationTabBar];
[self.window makeKeyAndVisible];
return YES;
}
-(void)creationTabBar
{
UINavigationController *myNavigationController;
// Create initialized instance of UITabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = CGRectMake(0, 0, self.window.bounds.size.width, self.window.bounds.size.height);
// Create initialized instance of NSMutableArray to hold our UINavigationControllers
NSMutableArray *tabs = [[NSMutableArray alloc] init];
World_ViewController *WorldScreen;
Home_ViewController *HomeScreen;
Favourite_ViewController *FavouriteScreen;
Rank_ViewController *RankScreen;
Setting_ViewController *SettingScreen;
UILabel *item1, *item2, *item3, *item4, *item5;
// Create first UIViewController
WorldScreen = [[World_ViewController alloc] initWithNibName:#"World_ViewController" bundle:nil];
HomeScreen = [[Home_ViewController alloc] initWithNibName:#"Home_ViewController" bundle:nil];
FavouriteScreen= [[Favourite_ViewController alloc] initWithNibName:#"Favourite_ViewController" bundle:nil];
RankScreen = [[Rank_ViewController alloc] initWithNibName:#"Rank_ViewController" bundle:nil];
SettingScreen = [[Setting_ViewController alloc] initWithNibName:#"Setting_ViewController" bundle:nil];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
item1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 430, 64, 50)];
item2 = [[UILabel alloc] initWithFrame:CGRectMake(64, 430, 64, 50)];
item3 = [[UILabel alloc] initWithFrame:CGRectMake(128, 430, 64, 50)];
item4 = [[UILabel alloc] initWithFrame:CGRectMake(195, 430, 64, 50)];
item5 = [[UILabel alloc] initWithFrame:CGRectMake(256, 430, 64, 50)];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:HomeScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:WorldScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:FavouriteScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:RankScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];
myNavigationController = [[UINavigationController alloc] initWithRootViewController:SettingScreen];
[myNavigationController setNavigationBarHidden:YES];
[tabs addObject:myNavigationController];
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:#"tabbar.png"]];
[tabBarController setViewControllers:tabs];
[tabBarController setDelegate:self];
[item1 setText:#"item1"];
[item1 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item1];
item1.tag = 101;
[item2 setText:#"item2"];
[item2 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item2];
item2.tag = 102;
[item3 setText:#"item3"];
[item3 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item3];
item3.tag = 103;
[item4 setText:#"item4"];
[item4 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item4];
item4.tag = 104;
[item5 setText:#"item5"];
[item5 setBackgroundColor:[UIColor grayColor]];
[tabBarController.view addSubview:item5];
item5.tag = 105;
[self.window setRootViewController:self.viewController];
}
- (void)tabBarController:(UITabBarController *)tabBarControllerref didSelectViewController:(UIViewController *)viewController
{
UILabel *item1 = (UILabel*)[tabBarControllerref.view viewWithTag:101];
UILabel *item2 = (UILabel*)[tabBarControllerref.view viewWithTag:102];
UILabel *item3 = (UILabel*)[tabBarControllerref.view viewWithTag:103];
UILabel *item4 = (UILabel*)[tabBarControllerref.view viewWithTag:104];
UILabel *item5 = (UILabel*)[tabBarControllerref.view viewWithTag:105];
[item1 setFrame: CGRectMake(0, 430, 64, 50)];
[item2 setFrame: CGRectMake(64, 430, 64, 50)];
[item3 setFrame: CGRectMake(128, 430, 64, 50)];
[item4 setFrame: CGRectMake(195, 430, 64, 50)];
[item5 setFrame: CGRectMake(256, 430, 64, 50)];
if([tabBarControllerref selectedIndex] == 0)
{
[item1 setTextColor:[UIColor redColor]];
[item2 setTextColor:[UIColor greenColor]];
[item3 setTextColor:[UIColor greenColor]];
[item4 setTextColor:[UIColor greenColor]];
[item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 1)
{
[item1 setTextColor:[UIColor greenColor]];
[item2 setTextColor:[UIColor redColor]];
[item3 setTextColor:[UIColor greenColor]];
[item4 setTextColor:[UIColor greenColor]];
[item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 2)
{
[item1 setTextColor:[UIColor greenColor]];
[item2 setTextColor:[UIColor greenColor]];
[item3 setTextColor:[UIColor redColor]];
[item4 setTextColor:[UIColor greenColor]];
[item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 3)
{
[item1 setTextColor:[UIColor greenColor]];
[item2 setTextColor:[UIColor greenColor]];
[item3 setTextColor:[UIColor greenColor]];
[item4 setTextColor:[UIColor redColor]];
[item5 setTextColor:[UIColor greenColor]];
}
else if ([tabBarControllerref selectedIndex] == 4)
{
[item1 setTextColor:[UIColor greenColor]];
[item2 setTextColor:[UIColor greenColor]];
[item3 setTextColor:[UIColor greenColor]];
[item4 setTextColor:[UIColor greenColor]];
[item5 setTextColor:[UIColor redColor]];
}
}

issue in setting image of navigation bar while presentModalViewController

this is how i set image in my first class in viewDidLoad which is tableView
if ([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)]) {
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"header.png"] forBarMetrics:UIBarMetricsDefault];
}
now this how i go to detail view
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:dvController animated:YES];
in detail view's bar the image which i have set for navigation bar comes automatically there and everything works perfect
now my problem is that pushViewController is working perfectly images are getting displayed
but i get default image in presentModelViewController this is how i use it
- (void) modelappear
{
if (self.testModalViewController == nil)
{
TestModalViewController *temp = [[TestModalViewController alloc] initWithNibName:#"TestModalViewController" bundle:[NSBundle mainBundle]];
self.testModalViewController = temp;
[temp release];
}
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.testModalViewController];
[self presentModalViewController:nav animated:YES];
}
Note i just set buttons in inner hierarchy views
Can you please tell me what am i doing wrong? please give explanation with answer thank you so much
try like this ,
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
dvController.topViewController.navigationController.navigationBar.tintColor = [UIColor colorWithPatternImage:[UIIMage imageNamed:#"name.png"]];
[self presentModalViewController:nav animated:YES];
UIImage *image = [UIImage imageNamed:#"header.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
UIImage *image = [UIImage imageNamed:#"header.png"];
UIImageView *imageViewe = [[UIImageView alloc] initWithImage:image];
UILabel *tmpTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 3, 150, 40)];
[tmpTitleLabel setFont:[UIFont boldSystemFontOfSize:18]];
tmpTitleLabel.text = #"Add Manually";
tmpTitleLabel.backgroundColor = [UIColor clearColor];
tmpTitleLabel.textColor = [UIColor whiteColor];
CGRect applicationFrame = CGRectMake(0, 0, 300, 40);
UIView * newView = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
[newView addSubview:imageViewe];
[newView addSubview:tmpTitleLabel];
[self.navigationController.navigationBar addSubview:newView];

iOS TabBarController background color

I have a tabbarcontroller that is pushed onto a navigationController. I tried to change the background color of the tab bar however it does not work:
UIViewController *viewController1, *viewController2;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
viewController1 = [[Tab1_iPhone alloc] initWithNibName:#"tab1_iPhone" bundle:nil];
viewController2 = [[Tab2_iPhone alloc] initWithNibName:#"tab2_iPhone" bundle:nil];
}
self.tabBarController = [[UITabBarController alloc] init];
CGRect frame = CGRectMake(0.0, 0.0, 480, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor blueColor]]; //003366
[v setAlpha:1.0];
[[self.tabBarController tabBar] insertSubview:v atIndex:0];
self.tabBarController.viewControllers = [NSArray arrayWithObjects: viewController1, viewController2, nil];
[self.navigationController setNavigationBarHidden:YES];
[self.navigationController pushViewController:self.tabBarController animated:YES];
[self.window makeKeyAndVisible];
This is the code I saw on a similar post that changes the bg color:
CGRect frame = CGRectMake(0.0, 0.0, 480, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[UIColor blueColor]]; //003366
[v setAlpha:1.0];
[[self.tabBarController tabBar] insertSubview:v atIndex:0];
Am I doing something wrong?
Thanks
The below code helps you to add custom colors with RGB values to ur tabBar.
self.tabBarController.tabBar.tintColor = [[UIColor alloc] initWithRed:0.00
green:0.62
blue:0.93
alpha:1.0];

Resources