How to set rootViewController for navigation view controller and tabbar controller in AppDelegate - ios

i have this problem. When i set rootViewController for TabbarController, it show correctly. But i set another rootViewController for navigation bar, TabbarController will not able to display. Any idea?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[DCTabBarController alloc] init];
DCTabBarController *tabBar = (DCTabBarController *)self.window.rootViewController;
[tabBar setSelectedIndex:2];
Map_ViewController *vc = [[Map_ViewController alloc] init];
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:vc];
[rootNav.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbarBackImage"] forBarMetrics:UIBarMetricsDefault];
rootNav.navigationBar.tintColor = [UIColor whiteColor];
[rootNav.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
LeftViewController *leftVC = [[LeftViewController alloc] init];
RightViewController *rightVC = [[RightViewController alloc] init];
XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:rootNav];
slideMenu.leftViewController = leftVC;
slideMenu.rightViewController = rightVC;
self.window.rootViewController = slideMenu;
[self.window makeKeyAndVisible];
return YES;
}
After applied Adeel solution, here is the output.But items in tabbar will not display accordingly.

One important thing to mention here is that an application's window can have only one rootViewController (of course). Like I said in my comment as well, you probably want to do something like this.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
Map_ViewController *vc = [[Map_ViewController alloc] init];
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:vc];
[rootNav.navigationBar setBackgroundImage:[UIImage imageNamed:#"navbarBackImage"] forBarMetrics:UIBarMetricsDefault];
rootNav.navigationBar.tintColor = [UIColor whiteColor];
[rootNav.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
DCTabBarController *tabBar = [[DCTabBarController alloc] init];
[tabBar setViewControllers:#[rootNav]];
self.window.rootViewController = tabBar;
LeftViewController *leftVC = [[LeftViewController alloc] init];
RightViewController *rightVC = [[RightViewController alloc] init];
XLSlideMenu *slideMenu = [[XLSlideMenu alloc] initWithRootViewController:tabBar];
slideMenu.leftViewController = leftVC;
slideMenu.rightViewController = rightVC;
self.window.rootViewController = slideMenu;
[self.window makeKeyAndVisible];
return YES;
}

Related

Can't get the item images on my tab bar to show

I have tried to code a tab view controller in my AppDelegate and I can either change my background color and have no tab bar at the bottom of the screen, or have no background color and an empty tab bar at the bottom.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController.tabBar setHidden:NO];
self.firstViewController = [[FirstViewController alloc] init];
[self.firstViewController.view setBackgroundColor:[UIColor redColor]];
self.secondViewController = [[SecondViewController alloc] init];
self.topTracksController = [[TopTracksViewController alloc] init];
self.settingsViewController = [[SettingsViewController alloc] init];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
[self.window setRootViewController:self.tabBarController];
[self.window makeKeyAndVisible];
return YES;
}
That is my code inside my AppDelegate currently, and I have items with images in my storyboard already.

MMDrawerController with UITabBarController showing a black gap on top of the center view

I am opening a UITabBarController with MMDrawerController on the left side.
So from my delegate didFinishLaunchingWithOptions I am making a MMDrawerController with centerNav is a TabBarController and leftNav is a ViewController with Navigation controller. So as expected the tabbar is coming with the mmdrawer but there is a black gap on top of the LandingTabBarController.If I open the LandingTabBarController directly then it is coming from the top. Whats going wrong?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LandingTabBarController *landingTabBarController = [[LandingTabBarController alloc]initWithManagedObjectContext:self.managedObjectContext];
UIViewController * drawerNavController = [storyboard instantiateViewControllerWithIdentifier:#"DrawerViewController"];
UINavigationController *centerNav = [[UINavigationController alloc]initWithRootViewController:landingTabBarController];
UINavigationController *leftNav = [[UINavigationController alloc]initWithRootViewController:drawerNavController];
centerNav.navigationBar.barStyle = UIStatusBarStyleLightContent;
leftNav.navigationBar.barStyle = UIStatusBarStyleLightContent;
_drawerController = [[MMDrawerController alloc] initWithCenterViewController:centerNav leftDrawerViewController:leftNav];
_drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView;
_drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModePanningCenterView;
_drawerController.showsShadow = NO;
//[_drawerController setDrawerVisualStateBlock:[MMDrawerVisualState slideVisualStateBlock]];
[_drawerController setDrawerVisualStateBlock:MMDrawerSideNone];
self.window.rootViewController = _drawerController;
[self.window makeKeyAndVisible];
return YES;
}
This is my LandingTabBarController viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
UIViewController *firstViewController = [[UIViewController alloc]init];
[firstViewController.view setBackgroundColor:[UIColor redColor]];
UIViewController *secondViewController = [[UIViewController alloc]init];
[secondViewController.view setBackgroundColor:[UIColor greenColor]];
[tabViewControllers addObject:firstViewController];
[tabViewControllers addObject:secondViewController];
[self setViewControllers:tabViewControllers];
firstViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"one" image:[UIImage imageNamed:#"icon_feed"] tag:1];
secondViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"two" image:[UIImage imageNamed:#"icon_apps"] tag:2];
}
Screenshot of the debugger

created Tab bar and side menu Issues

I created tab bar and side menu using Sw reveal programmatically but after implementing all process i cant able to view side menu and my tab bar also disappeared...i don't know how to alloc them pleas help me out
#implementation AppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.tabBarController =[[UITabBarController alloc]init];
//Initialize View controller and speciality
UIViewController *viewcontroller1=[[HomeView alloc]init];
UIViewController *viewcontroller2=[[Speciality alloc]init];
UIViewController *viewcontroller3=[[Activity alloc]init];
UIViewController *viewcontroller4 =[[Notification alloc]init];
UIViewController *viewcontroller5 =[[Profile alloc]init];
self.tabBarController.viewControllers=[NSArray arrayWithObjects:viewcontroller1,viewcontroller2,viewcontroller3,viewcontroller4,viewcontroller5, nil];
self.window.rootViewController =self.tabBarController;
self.tabBarController.tabBar.barTintColor = [UIColor colorWithRed:0.376 green:0.729 blue:0.318 alpha:1.000];
self.window.backgroundColor = [UIColor whiteColor];
HomeView *frontViewController = [[HomeView alloc] init];
RearViewController *rearViewController = [[RearViewController alloc] init];
UINavigationController *frontNavigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
UINavigationController *rearNavigationController = [[UINavigationController alloc] initWithRootViewController:rearViewController];
SWRevealViewController *mainRevealController = [[SWRevealViewController alloc]initWithRearViewController:rearNavigationController frontViewController:frontNavigationController];
mainRevealController.delegate = self;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
You set your window.rootViewController two times. And it seems the second time you assign nil to it, as I can't see any initialization/assignement to self.viewController.

How to programmatically add a UITabBarController & UINavigationController in AppDelegate?

How to add a UINavigationController & UITabBarController programmatically in app delegate.
Don't forget in the AppDelegate.h file to add:
#property (strong, nonatomic) UITabBarController *tabBarController;
Below is the AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.tabBarController = [[UITabBarController alloc] init];
ViewController1 *VC1 = [[ViewController1 alloc] init];
VC1.title = #"Tab Title Here";
UINavigationController *VC1Navigation = [[UINavigationController alloc]
initWithRootViewController:VC1];
ViewController2 *VC2 = [[ViewController2 alloc] init];
VC2.title = #"Tab Title Here";
UINavigationController *VC2Navigation = [[UINavigationController alloc]
initWithRootViewController:VC2];
ViewController3 *VC3 = [[ViewController3 alloc] init];
homeView.title = #"Tab Title Here";
UINavigationController* VC3Navigation = [[UINavigationController alloc]
initWithRootViewController:VC3];
NSArray* controllers = [NSArray arrayWithObjects:VC1Navigation, VC2Navigation, VC3Navigation, nil];
self.tabBarController.viewControllers = controllers;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}

iOS 7 - TabBarController does not have text label

I am trying to create an application with a UITabBarController. I have followed several tutorials online and it seems like I'm missing something since my UITabBarController does not have text but you can switch from one View to another.
Here is my didFinishWithLaunchingOptions function:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
ListViewController *listVC = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
self.tabController.viewControllers = [[NSArray alloc] initWithObjects:viewController1, listVC, nil];
self.window.rootViewController = self.tabController;
[self.window makeKeyAndVisible];
[self createCopyOfDatabaseIfNeeded];
return YES;
}
What am I missing out on?
You need to set the tab bar image and title of each view controller:
self.tabController = [[UITabBarController alloc] init];
UIViewController *viewController1 = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
ListViewController *listVC = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
viewController1.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"VC1" image:[UIImage imageNamed:#"tab_ yourViewController.png"] tag:0];
listVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"listVC" image:[UIImage imageNamed:#"tab_listvc.png"] tag:1];
self.tabController.viewControllers = [[NSArray alloc] initWithObjects:viewController1, listVC, nil];
It's also possible (and often preferred) to set the tabBarItem for each view controller in viewDidLoad, I.e.:
- (void)viewDidLoad
{
[super viewDidLoad]; // Be a good citizen!
self.tabBarItem = [[UITabBarItem alloc] init...];
...
You can set tabbar item for each view with code
UITabBar *tabBar = self. tabController.tabBar;
UITabBarItem *tabBarItem0 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:1];
[tabBarItem0 setTitle:#"title1"];
[tabBarItem0 setImage:[UIImage imageNamed:#"img1"]];
[tabBarItem1 setTitle:#"title2"];
[tabBarItem1 setImage:[UIImage imageNamed:#"img2"]];
self.window.rootViewController = self.tabController;
[self.window makeKeyAndVisible];

Resources