I want the default height as seen in the Storyboard image when adding the bar programmatically. Any idea how to get it? Here is the code I use to add a UITabBarController to my app.
- (void)setCustomTabBar
{
//TODO: Fix TabBar height
UITabBarController *tabBarController = [[UITabBarController alloc] init];
TimelineVC *tvc = [[TimelineVC alloc] initWithNibName:#"TimelineVC" bundle:nil];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:tvc];
TimelineVC *tvc2 = [[TimelineVC alloc] initWithNibName:#"TimelineVC" bundle:nil];
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:tvc2];
tvc2.showFriends = true;
NotificationVC *nvc = [[NotificationVC alloc] initWithNibName:#"NotificationVC" bundle:nil];
nav3 = [[UINavigationController alloc] initWithRootViewController:nvc];
ProfileVC *pvc = [[ProfileVC alloc] initWithNibName:#"ProfileVC" bundle:nil];
UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:pvc];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setShadowImage:nil];
[tabBarController setViewControllers:#[nav1, nav2, nav3, nav4]];
tabBarController.selectedIndex = 0;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat tabBarWidth = screenRect.size.width/4;
[[UITabBar appearance] setBackgroundImage:[AppDelegate imageFromColor:[UIColor darkGrayColor] forSize:CGSizeMake(screenRect.size.width, 49) withCornerRadius:0]];
[[UITabBar appearance] setSelectionIndicatorImage:[AppDelegate imageFromColor:[UIColor colorWithRed:26/255.0 green:163/255.0 blue:133/255.0 alpha:1] forSize:CGSizeMake(tabBarWidth, 49) withCornerRadius:0]];
tabBarController.tabBar.translucent = NO;
UIImage *normalImage, *selectedImages;
normalImage =[UIImage imageNamed:#"TimeLineIcon"];
selectedImages = [UIImage imageNamed:#"TimeLineIcon"];
UIImage *normalImage2, *selectedImages2;
normalImage2 =[UIImage imageNamed:#"FriendsIcon"];
selectedImages2 = [UIImage imageNamed:#"FriendsIcon"];
UIImage *normalImage3, *selectedImages3;
normalImage3 =[UIImage imageNamed:#"NotificationIcon"];
selectedImages3 = [UIImage imageNamed:#"NotificationIcon"];
UIImage *normalImage4, *selectedImages4;
normalImage4 =[UIImage imageNamed:#"ProfileIcon"];
selectedImages4 = [UIImage imageNamed:#"ProfileIcon"];
//TODO: FOR LOOP
nav1.tabBarItem.selectedImage = [normalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav1.tabBarItem.image = [selectedImages imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav1.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
nav2.tabBarItem.selectedImage = [normalImage2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav2.tabBarItem.image = [selectedImages2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav2.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
nav3.tabBarItem.selectedImage = [normalImage3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav3.tabBarItem.image = [selectedImages3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav3.tabBarItem.badgeValue = #"1";
nav3.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
nav4.tabBarItem.selectedImage = [normalImage4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav4.tabBarItem.image = [selectedImages4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
nav4.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
self.window.rootViewController = tabBarController;
[self.window addSubview:tabBarController.view];
[self enableSupportKit];
}
[self.tabBar setFrame:CGRectMake(self.tabBar.frame.origin.x, self.tabBar.frame.origin.y, self.tabBar.frame.size.width, self.tabBar.frame.size.height)];
This helped me.
Hope it helps.
checkout your launchImage.for example,it's correct in your simulator,but if you use your iphone(i guess is iphone6). remove iphone6 launchimage,then you will find the question.
Related
I am working in UITabBarController, when I am going to set image for tabs, the image does not appear for first time when view is open. I don't know how to resolve this problem.
This is the code I am using:
UIImage *prfimage = [UIImage imageNamed:#"profile_o.png"];
UIImage *selimage = [UIImage imageNamed:#"profile_o.png"];
UITabBarItem *tbP = [[UITabBarItem alloc] initWithTitle:#"" image:prfimage tag:YES];
[self setTabBarItem:tbP];
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:247/255.0f green:148/255.0f blue:29/255.0f alpha:2.0f]];
self.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
self.title = nil;
by following way you can set.
UITabBarItem *tabItem = controller.tabBarItem;
tabItem.image = [[UIImage imageNamed:#"history"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
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 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.
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];
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];