简体   繁体   中英

iOS - Access Navigation Controller in AppDelegate

I have built my app using Storyboards. I have a UITabBarController and a NavigationController embedded it that. I am trying to customise my app with tint colors and I have managed to set a custom color for the Tabbar. I was able to access the tabbar from the rootViewController, however I cannot get access to the navigation bar. Is there anyway that I can easily access this? Sample of my AppDelegate code is below.

Thanks

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:  (NSDictionary *)launchOptions
{
    self.tabBarController = (UITabBarController*)self.window.rootViewController;
    UITabBar *tabBar = self.tabBarController.tabBar;
    [tabBar setSelectedImageTintColor:[UIColor greenColor]];
    tabBar.tintColor = [UIColor colorWithRed:0.4 green:0.522 blue:0.129 alpha:1];

    //This bit doesnt work
    UINavigationController *navCon = self.tabBarController.navigationController;
    navCon.navigationBar.tintColor = [UIColor colorWithRed:0.384 green:0.259 blue:0.161     alpha:1];

    return YES;    
}

If you're using navigation controllers, would expect them to embed the view controllers that are managed by the tab bar controller.

Try NSLog(@"%@", [self.tabBarController viewControllers]); and see whether it lists navigation controllers or your custom controller. For example, I created two view controllers that are triggered by my tab bar, one with and one without navigation and the above line shows:

 (
     "<UINavigationController: 0x6a35f20>",
     "<SecondViewController: 0x6a39500>" )

So, I could get mine as [[self.tabBarController viewControllers] objectAtIndex:0] .

Assuming you did no work with IBOutlet's, I suggest you maybe use them along with the XIB file. For example, In the header file:

IBOutlet UINavigationBar *aNavBar;

And then in the.m, switch:

UINavigationController *navCon = self.tabBarController.navigationController;
navCon.navigationBar.tintColor = [UIColor colorWithRed:0.384 green:0.259 blue:0.161     alpha:1];

With:

[aNavBar setTintColor:[UIColor colorWithRed:0.384 green:0.259 blue:0.161     alpha:1]];

And there you go, I made this quick. so tell me if it works or not.
- Justin A.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM