UITabbar Images and title are not center aligned in ipad - ios

I am working on application which uses Tabbarcontroller (There are two tabs )
Everything works fine on iphone but on ipad both the tabs comes in center of the screen
I want to show the title and images in center of each tab (considering the tab width is half of the screen)
While searching for these I came across UIEdgeInsetsMake
Adjust iOS TabBar item title to the centre of it but no help with this.
I dont understand where to put this code. I tried to put it in FirstViewController and then in app delegate (one by one ) but nothing works for me
Here is my appDelegete Code :
FirstViewController *firstVC = [[FirstViewController alloc] init];
UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:firstVC];
//navController1.title = #"First Tab";
navController1.tabBarItem.image = [UIImage imageNamed:#"first.png"];
SecondViewController *secondVC = [[SecondViewController alloc] init];
UINavigationController *navController2 = [[UINavigationController alloc] initWithRootViewController:secondVC];
navController2.title = #"Second Tab";
navController2.tabBarItem.image = [UIImage imageNamed:#"second.png"];
NSArray *array = [[NSArray alloc] initWithObjects:navController1,navController2, nil];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.tabBarItem.imageInsets = UIEdgeInsetsMake(0, -50, 0, 50);
tabBarController.viewControllers = array;
[self.window setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
Can anybody help ?

You should put this code in viewWillLayoutSubviews of your UITabBarController
self.tabBar.itemPositioning = UITabBarItemPositioningFill;
Swift 4, 5
self.tabBar.itemPositioning = .fill

With reference to this answer I would suggest you to do like this
1 Get the screen width and divide it by the number of tab bar items you have
2 Once divided assgin the mean value via setItemWidth to the UITabBar
CGRect screenSize = [[UIScreen mainScreen] applicationFrame];
int count = [array count];
float width = screenSize.size.width/count;
[[UITabBar appearance] setItemWidth:width];

Related

Adding a persistent footer underneath a UITabBarController

I've inherited a project for which I want to add a persistent footer area beneath the entire app. The app is using a UITabBarController that is always shown other than for a login screen. The tabBarController is created as follows:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
UIStoryboard *sb1 = [UIStoryboard storyboardWithName:#"SB1" bundle:nil];
// ... same for sb2 and sb3
[tabBarController setViewControllers:#[sb1, sb2, sb3]];
[tabBarController setSelectedIndex:0];
I've tried manually setting tabBarController.size.height, but it doesn't seem to have any effect. I've never used storyboards before, is there some way to do initWithFrame when using them? Or am I approaching this completely the wrong way?
Thanks werediver, you got me on the right track. I got it working by adding the UITabBarController as a child view controller and then manually adding its view as well.
const CGFloat FOOTER_HEIGHT = 20;
// Set up tab bar area
CGRect contentFrame = [UIApplication sharedApplication].delegate.window.frame;
contentFrame.size.height -= FOOTER_HEIGHT;
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.view.frame = contentFrame;
// Set up footer
CGRect footerFrame = contentFrame;
footerFrame.origin.y = contentFrame.size.height;
footerFrame.size.height = FOOTER_HEIGHT;
UIView *footer = [[UIView alloc] initWithFrame:footerFrame];
// Create outer view controller
UIViewController *outer = [[UIViewController alloc] init];
[outer addChildViewController:tabBarController];
[outer.view addSubview:tabBarController.view];
[outer.view footer];
[[UIApplication sharedApplication] keyWindow].rootViewController = outer;

uiview issue with tab bar

I am working on Xcode 5.1.2 iOS7.My problem is that i want to implement tab bar controller on second view controller . Tab bar controller is implemented successfully.My all view are slightly transparent which inserts in array to tab bar.when i click on tab bar button,first view is visible at behind of every view of tab bar views.so how i can solve this problem.
tabbar=[[UITabBarController alloc]init];
UIViewController *first=[[ViewController alloc]init];
UIViewController *second=[[SearchViewController alloc]init];
UIViewController *third=[[Party_partner_ViewController alloc]init];
UIViewController *Fourth=[[My_GrooveViewController alloc]init];
UIViewController *Fifth=[[More_ViewController alloc]init];
UINavigationController *firstNavController = [[UINavigationController alloc] initWithRootViewController:first];
UINavigationController *firstNavController1 = [[UINavigationController alloc] initWithRootViewController:second];
UINavigationController *firstNavController2 = [[UINavigationController alloc] initWithRootViewController:third];
UINavigationController *firstNavController3 = [[UINavigationController alloc] initWithRootViewController:Fourth];
UINavigationController *firstNavController4 = [[UINavigationController alloc] initWithRootViewController:Fifth];
NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
if ([[ver objectAtIndex:0] intValue] >= 7) {
self.nav.navigationBar.barTintColor = [UIColor colorWithRed:29/255.0f green:29/255.0f blue:29/255.0f alpha:1.0f];
self.nav.navigationBar.translucent = NO;
}else
{
self.nav.navigationBar.tintColor = [UIColor colorWithRed:29/255.0f green:29/255.0f blue:29/255.0f alpha:1.0f];
}
self.nav.navigationBar.topItem.title = #"The Groove";
tabbar.viewControllers=[NSArray arrayWithObjects:firstNavController,firstNavController1, firstNavController2,firstNavController3,firstNavController4, Nil];
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
// UIWindow *window= [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[mainDelegate.window addSubview:tabbar.view];
sure first you can hide or remove your uiview

how to add tab bar controller from the second view controller [duplicate]

This question already has an answer here:
Showing login view controller before main tab bar controller
(1 answer)
Closed 9 years ago.
Im beginner to IOS app development learning.
I have a login screen as my first view controller and i need the second view controller to be a tab bar view controller .with 4 different tabs and i have 4 different XIB's for them.
some one help me to go ahead.
Best way you can do is Present the login screen modally when the app starts from your tab bar controller first screen, add code for presenting login screen in viewWillAppear and after login dismiss the screen. You can create TabBarController in appDelegate like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController tabBarController=[[UITabBarController alloc] init];
FirstViewController *firstVC = [[UIViewController alloc] initWithNibName:#"FirstVC" bundle:nil];
UINavigationController *firstNavController = [[UINavigationController alloc] initWithRootViewController: firstVC];
SecondViewController *secondVC = [[UIViewController alloc] initWithNibName:#"secondVC" bundle:nil];
UINavigationController *secondNavController = [[UINavigationController alloc] initWithRootViewController:secondVC];
tabBarController.viewControllers = [NSArray arrayWithObjects: firstNavController, secondNavController, nil];
tabBarController.selectedIndex=0;
tabBarController.delegate = self;
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:#"Movies" image:[UIImage imageNamed:#"MoviesTAB.png"] tag:1];
[firstVC setTabBarItem:item1];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:#"Music" image:[UIImage imageNamed:#"musicTAB.png"] tag:2];
[seconfVC setTabBarItem:item2];
tabController.tabBar.translucent = NO;
tabController.tabBar.barStyle = UIBarStyleBlack;
tabBarController.tintColor = [UIColor whiteColor];
self.window.rootViewController = tabController;
return YES;
}
Best way is use storyboard and there just have one initial UIViewController and from that make segue to UITabBarViewController.
http://youtu.be/a_DCTSTv1Mw
If you want to make it through xib make a UITabBarViewController and add viewControllers to the array of object of that UITabBarViewController's object.
Sample code :
NSMutableArray *arrViewControllers = [[NSMutableArray alloc] init];
UIViewController *tabController;
UIImage *tabImage ;
NSString *tabTitle;
for (int i= 0; i < 3; i++) {
switch (i) {
case 0:
tabController = [[ViewController alloc] init];
tabImage = [UIImage imageNamed:#"icon1.png"];
tabTitle = #"Text";
break;
case 1:
tabController = [[ImageDemoViewController alloc] init];
tabImage = [UIImage imageNamed:#"icon2.png"];
tabTitle = #"Image";
break;
case 2:
tabController = [[TableDemoViewController alloc] init];
tabImage = [UIImage imageNamed:#"icon3.png"];
tabTitle = #"Table";
break;
}
// set the image and title using some properties of UIViewController
tabController.tabBarItem.image = tabImage;
tabController.tabBarItem.title = tabTitle;
//add objects to array
[arrViewControllers addObject:tabController];
[tabController release];
}
_baseController = [[UITabBarController alloc] init];
_baseController.viewControllers = arrViewControllers;
[arrViewControllers release];
go to your appDelegate
1.create a viewController for login screen.
LoginViewController *viewController1 = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
2.create a navigationController with root view your login ViewController.
UINavigationController *nVC = [[UINavigationController alloc] initWithRootViewController:viewController1];
3.make navigationController to root view of window.
self.window.rootViewController = self.nVC;
[self.window makeKeyAndVisible];
Now go to Touch-Up-Inside method of login button in LoginViewController.
1.After validation of password and userId initialise your viewControllers for tabbar and TabbarViewController.
UiViewController ...*yourViewControllers,..,
UITabBarController *YourtabBarController = [[UITabBarController alloc] init];
2.Now add these viewControllers to your tabbarController.
YourtabBarController.viewControllers = #[ YourViewController1,YourViewController2,YourViewController3,......];
3.Finally push this tabbarController to navigationControllere.
[self.navigationController pushViewController:YourtabBarController animated:NO];

Tab Bar not hiding Using DDMenuController (Fb like sliding menu)

I am facing this issue from last two days, but could find any solution on it. Can some one help. This is the code snippet am using for TabBar viewControllers.
// Set Up Tab Bar
NSMutableArray *tabBarViewControllers = [[NSMutableArray alloc] initWithCapacity:5];
self.tabBarController = [[UITabBarController alloc] init];
[tabBarController
setDelegate:self];
UINavigationController *navController = nil;
NSArray *vcArray = [self papulateViewControllers];
// SetViewController for tab Bar
-(NSArray *) papulateViewControllers{
BrowseViewController *browseVC = [[BrowseViewController alloc] initWithNibName:#"BrowseViewController" bundle:nil];
AlbumViewController *albumVC = [[AlbumViewController alloc] initWithNibName:#"AlbumViewController" bundle:nil];
SubmitStep1VC *submitVC = [[SubmitStep1VC alloc] initWithNibName:#"SubmitStep1VC" bundle:nil];
// SubmitStep1VC *submitVC = [[SubmitStep1VC alloc] initWithNibName:#"SubmitStep1_iPhone5.xib" bundle:[NSBundle mainBundle]];
WallViewController *wallVC = [[WallViewController alloc] initWithNibName:#"WallViewController" bundle:nil];
OptionVC *optionVC = [[OptionVC alloc] initWithNibName:#"OptionVC" bundle:nil];
sliderVCRef = [[SliderVC alloc] initWithNibName:#"SliderVC" bundle:nil];
//Navigation Controllers
UINavigationController *browseNavController = [[UINavigationController alloc] initWithRootViewController: browseVC];
[browseNavController setNavigationBarHidden:YES];
UINavigationController *albumNavController = [[UINavigationController alloc] initWithRootViewController: albumVC];
[albumNavController setNavigationBarHidden:YES];
UINavigationController *submitNavController = [[UINavigationController alloc] initWithRootViewController: submitVC];
[submitNavController setNavigationBarHidden:YES];
UINavigationController *wallNavController = [[UINavigationController alloc] initWithRootViewController: wallVC];
[wallNavController setNavigationBarHidden:YES];
UINavigationController *optionNavController = [[UINavigationController alloc] initWithRootViewController: optionVC];
[optionNavController setNavigationBarHidden:YES];
DDMenuController *browseMenuController = [[DDMenuController alloc] initWithRootViewController:browseNavController];
self.menuController = browseMenuController;
self.menuController.leftViewController = sliderVCRef;
DDMenuController *albumMenuController = [[DDMenuController alloc] initWithRootViewController:albumNavController];
albumMenuController.leftViewController = sliderVCRef;
DDMenuController *submitMenuController = [[DDMenuController alloc] initWithRootViewController:submitNavController];
submitMenuController.leftViewController = sliderVCRef;
DDMenuController *wallMenuController = [[DDMenuController alloc] initWithRootViewController:wallNavController];
wallMenuController.leftViewController = sliderVCRef;
DDMenuController *optionMenuController = [[DDMenuController alloc] initWithRootViewController:optionNavController];
optionMenuController.leftViewController = sliderVCRef;
/// Works fine if i uncomment this line and comment next line of code (Passing Viewcontrollers is fine )
// return [NSArray arrayWithObjects:self.menuController, albumVC, submitVC, wallVC, optionVC, nil];
////******* issue in case i use this line (Passing menuController creates issue of Tabbar )
return [NSArray arrayWithObjects:self.menuController, albumMenuController, submitMenuController, wallMenuController, optionMenuController, nil];
////////////
}
When i try to push to push to any viewcontroler from any above TabBarController Tab bar is not hiding . example
grandPrizeVC.hidesBottomBarWhenPushed = YES;
Its keep showing me tab bar.
If i try
appDelegate.tabbarcontroller.tabbar.hidden = YES; It shows on a black bottom bar on new VC.
Your app doing just what you have implemented. You are adding your sliding menu view controller as subview controller to tabbar controller, of course, it won't hide. Some suggestions to hide tabbar:
1. Add tabbar controller as modal to your DDMenuController
2. write some methods to hide/show tabbar (searching hide tabbar will give you the answers, or you could just traverse subviews of tabbar controller's view find tabbar and hide it).
Good luck!
Following worked for me :
Show TabBar :
+ (void) showTabBar{
MyAppDelegate* appDelegate = (MyAppDelegate *) [[UIApplication sharedApplication]delegate];
UITabBar *tabBar = appDelegate.tabBarController.tabBar;
UIView *parent = tabBar.superview; // UILayoutContainerView
UIView *content = [parent.subviews objectAtIndex:0]; // UITransitionView
UIView *window = parent.superview;
CGRect tabFrame = tabBar.frame;
tabFrame.origin.y = CGRectGetMaxY(window.bounds) - CGRectGetHeight(tabBar.frame);
tabBar.frame = tabFrame;
CGRect contentFrame = content.frame;
contentFrame.size.height -= tabFrame.size.height;
}
Hide TabBar:
+(void) hideTabBar{
MyAppDelegate* appDelegate = (MyAppDelegate *) [[UIApplication sharedApplication]delegate];
UITabBar *tabBar = appDelegate.tabBarController.tabBar;
UIView *parent = tabBar.superview; // UILayoutContainerView
UIView *content = [parent.subviews objectAtIndex:0]; // UITransitionView
UIView *window = parent.superview;
[UIView animateWithDuration:0.01
animations:^{
CGRect tabFrame = tabBar.frame;
tabFrame.origin.y = CGRectGetMaxY(window.bounds);
tabFrame.origin.y +=20;
tabBar.frame = tabFrame;
content.frame = window.bounds;
}];
}

Toolbar + Navigation Bar + Segmented Control?

I'm trying to find a way to layout an application that includes a tab bar on the bottom, a navigation bar on the top, and a row of buttons on the navigation bar that switches views (on the first tab).
I've drawn a very rough sketch (sorry!), but I hope it illustrates the intent.
On the bottom, there are two tabs (tab1, and tab2).
When Tab1 is selected, the navigation bar will have 3 buttons that will show different views (tab1_1, tab1_2, tab1_3).
When Tab2 is selected, the navigation bar won't show any buttons, but rather some simple text.
At this point, I have the following scheme in my application delegate's didFinishLaunchingWithOptions:
UIViewController *viewController1 = [[Tab1_ViewController alloc] initWithNibName:#"Tab1_ViewController" bundle:nil];
UIViewController *viewController2 = [[Tab2_ViewController alloc] initWithNibName:#"Tab2_ViewController" bundle:nil];
tab1NavController = [[UINavigationController alloc] initWithRootViewController:viewController1];
tab2NavController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:tab1NavController, tab2NavController, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
I was wondering if I need to redo how I'm doing things in order to achieve the layout as in the picture.
Any help would be appreciated, thank you!
I have done this for my current project...i hope this will help you....
At first take UITabbarController at your first viewController [first sketch you have given]
For your first view use this code....
- (void)viewDidLoad {
[super viewDidLoad];
dashBoardView = [[DashboardViewController alloc] initWithNibName:#"DashboardViewController" bundle:nil];
dashBoardView.title = #"dashBoardView";
UINavigationController *mydashboarController = [[[UINavigationController alloc] initWithRootViewController:dashBoardView] autorelease];
mydashboarController.navigationBar.barStyle = UIBarStyleBlack;
[listOfViewControllers addObject:mydashboarController];
[dashBoardView release];
ordersView = [[OrdersViewController alloc] initWithNibName:#"OrdersViewController" bundle:nil];
ordersView.title = #"ordersView";
UINavigationController *myorderController = [[[UINavigationController alloc] initWithRootViewController:ordersView] autorelease];
myorderController.navigationBar.barStyle = UIBarStyleBlack;
[listOfViewControllers addObject:myorderController];
[ordersView release];
orderList = [[OrderListViewController alloc] initWithNibName:#"OrderListViewController" bundle:nil];
orderList.title = #"orderList";
UINavigationController *myorderListController = [[[UINavigationController alloc] initWithRootViewController:orderList] autorelease];
myorderListController.navigationBar.barStyle = UIBarStyleBlack;
[listOfViewControllers addObject:myorderListController];
[orderList release];
productView = [[ProductViewController alloc] initWithNibName:#"ProductViewController" bundle:nil];
productView.title = #"productView";
UINavigationController *myproductController = [[[UINavigationController alloc] initWithRootViewController:productView] autorelease];
[listOfViewControllers addObject:myproductController];
[productView release];
[self.tabBarController setViewControllers:listOfViewControllers animated:YES];
NSArray *segmentTextContent = [NSArray arrayWithObjects:NSLocalizedString(#"Dashboard", #""),NSLocalizedString(#"Order", #""),
NSLocalizedString(#"Product", #""),NSLocalizedString(#"More", #""),
nil];
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 400, 40);
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//defaultTintColor = [segmentedControl.tintColor retain]; // keep track of this for later
segmentedControl.tintColor = [UIColor colorWithHue:8.0 saturation:8.0 brightness:8.0 alpha:1.0];
segmentedControl.alpha = 0.8;
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];
}
If it is not clear to you then please knock...

Resources