How to reorder TabBar's Tabs programmatically? - ios

Is there a way to reorder tabs of a TabBar programmatically on xCode?
Thank you a lot, Matteo!

Try this:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
tabBarController = [[UITabBarController alloc] init];
MyViewController* vc1 = [[MyViewController alloc] init];
MyOtherViewController* vc2 = [[MyOtherViewController alloc] init];
NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
tabBarController.viewControllers = controllers;
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
}

Related

How to display all tabBar titles in ios7

Basically i cant get to display all tabBar Items when i run my app, just the first view controller is displayed:
I literally have to click on a tab to display its Item:
This my code in Appdelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Initialize window
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
// Set background colors for both NavBar and TabBar
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0.157 green:0.718 blue:0.553 alpha:1]];
[[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:0.141 green:0.216 blue:0.263 alpha:1]];
// Initialize your five tab controllers. with each tab has its own navigation controller
HomePageView *homePageView = [[HomePageView alloc]init];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:homePageView];
ProfileViewController *profileViewController=[[ProfileViewController alloc]init];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:profileViewController];
FeedViewController *feedViewController=[[FeedViewController alloc]init];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:feedViewController];
ListeningSessionViewController *listeningSessionViewController= [[ListeningSessionViewController alloc]init];
UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:listeningSessionViewController];
RecievedViewController *recievedViewController =[[RecievedViewController alloc]init];
UINavigationController *nav5 = [[UINavigationController alloc]initWithRootViewController:recievedViewController];
// initialize tabbarcontroller,set your viewcontrollers and change its color.
self.tabC = [[UITabBarController alloc]init];
NSArray* controllers = [NSArray arrayWithObjects: nav1,nav2,nav3,nav4,nav5, nil];
[self.tabC setViewControllers: controllers animated:NO];
[_window addSubview:self.tabC.view];
// Show window
[self.window makeKeyAndVisible];
return YES;
}
I'm guessing that you're setting the titles in the viewDidLoad or viewDidAppear methods of the controllers. That won't work, because, while all the controllers are instantiated in the app delegate, only the controller at index 0 has its view loaded, and thus viewDidLoad will not be run for the other controllers. Instead, you should set the titles on the navigation controllers in the app delegate,
ProfileViewController *profileViewController=[[ProfileViewController alloc]init];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:profileViewController];
nav2.tabBarItem.title = #"Profile";

Getting a UITabBar after breaking out of a UINavigationBar

I have a UINavigationBar that has an AuthenticateViewController in it. Then when the user his Sign In in the upper right of my navigation control, I want to show a UITabBar controller. Do I still create this in appDelegate? How do I "break out" of the UINavigation controller?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Add methods for layout of this view controller here
//1
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
//2
self.managedObjectContext = appDelegate.managedObjectContext;
[self.navigationItem setHidesBackButton:YES];
// Now add the Sign In button
UIBarButtonItem *signinButton = [[UIBarButtonItem alloc] initWithTitle:#"Sign In" style:UIBarButtonItemStylePlain target:self action:#selector(signinButtonPressed:)];
self.navigationItem.rightBarButtonItem = signinButton;
}
- (void) signinButtonPressed:(UIBarButtonItem *) sender
{
// What goes here to start the UITabBars
}
Set your TabBarController as rootViewController after signed in.
Your button action would be like this:
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = #[<your viewControllers>];
AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate.window setRootViewController:tabBarController];
Edit: You might don't get use with array creation with #[]. So here is an explanation:
UIViewController *viewController1 = [[UIViewController alloc] init];
UIViewController *viewController2 = [[UIViewController alloc] init];
// Two methods of adding item to array
// First method
NSArray *array = [NSArray arrayWithObjects:viewController1, viewController2, nil];
// or
NSArray *array = #[viewController1, viewController2];
tabBarController.viewControllers = array;
You'll need to do a modalTransition to jump out of the navigationController.. this will keep the AuthenticateViewController as RootViewController of the window.. if you want to change the RootViewController as well then you'll need to get the AppDelegate's window and change its RootViewController to the TabBarController you'll make hereā€¦ hope you got it..

MHTabBarController add to uiviewcontroller

Im trying to use the MHTabBarController https://github.com/hollance/MHTabBarController
, in the example the custom view controller is added as a rootViewController in app delegate,
i want to add 3 tabs inside a standard tab bar controller's view for example MainViewController
here is my code inside viewDidLoad in MainViewController.m:
//MHTabBarController config
ListViewController *listViewController1 = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
ListViewController *listViewController2 = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
ListViewController *listViewController3 = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
listViewController1.title = #"Tab 1";
listViewController2.title = #"Tab 2";
listViewController3.title = #"Tab 3";
NSArray *viewControllers = #[listViewController1, listViewController2, listViewController3];
MHTabBarController *tabBarController = [[MHTabBarController alloc] init];
tabBarController.delegate = self;
tabBarController.viewControllers = viewControllers;
[self.view addSubview:tabBarController.view];
ive also added the in MainViewController.h
but its not working, its showing a table but not the tabs on top.
what im missing?
That code should go inside the AppDelegate.m
Something like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
MainViewController *mainViewController = [[MainViewController alloc] init];
mainViewController.title = #"Main Tab";
NSArray *viewControllers = #[mainViewController];
MHTabBarController *tabBarController = [[MHTabBarController alloc] init];
tabBarController.delegate = self;
tabBarController.viewControllers = viewControllers;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
This way you're adding a the tab bar into the MainViewController, if you wanna add the same tab bar into other view controllers, just add the view controllers into the viewControllers array.

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

iPad Navigation controller in splitview popover frame issue

I put a navigation controller as Split-view root-view controller.
When the iPad is in Portrait mode the root view controller is displayed inside a popover controller. Its by default I know....
But My problem is The top of root-view controller is seems hide by popover. See the attched image. In it the home bar-button seems hide a small portion in top.
RootViewController *objRootViewController = [[RootViewController alloc] init];
DetailViewController *objDetailViewController = [[DetailViewController alloc] init];
UINavigationController *rootNavigationController = [[UINavigationController alloc] initWithRootViewController:objRootViewController];
UINavigationController *detailNavigationController = [[UINavigationController alloc] initWithRootViewController:objDetailViewController];
UISplitViewController *objSplitViewController = [[UISplitViewController alloc] init];
[objSplitViewController setDelegate:objDetailViewController];
[objSplitViewController setViewControllers:[NSArray arrayWithObjects:rootNavigationController,detailNavigationController, nil]];
self.splitViewController = objSplitViewController;
[self.window addSubview:self.splitViewController.view];
[objSplitViewController release];
objSplitViewController = nil;
[rootNavigationController release];
rootNavigationController = nil;
[detailNavigationController release];
detailNavigationController = nil;
[objDetailViewController release];
objDetailViewController = nil;
[objRootViewController release];
objRootViewController = nil;

Resources