Test 87
Test 87

Reputation: 111

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

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. enter image description here

Upvotes: 0

Views: 197

Answers (1)

Adeel Miraj
Adeel Miraj

Reputation: 2496

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

Upvotes: 1

Related Questions