In my Delegate i am trying to select my TabBarController
so that i can style it with a different background. However the problem is that my TabBarController
is not located on the rootView..
My current code:
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
In my interface builder i have my TabBarController
setup with a Segue name: mainView (This is where the TabBarController
is located).
How could i select my TabBarController
?
First, you have to know in your view hierarchy where is your TabBarController. If it's not your root controller, Locate the UIViewController that are calling the TabBarController, and get it's reference by segue or something like it.
What might work for you, it's accessing the tabBarController
property in the viewDidLoad
of the first child UIViewController in a tab inside your tabViewController. All child ViewControllers of the tabBarController have this property.
For example, assuming first UIViewController displayed in the tabBar is MyViewController, perform something like this:
- (void)viewDidLoad
{
UITabBar *tabBar = self.tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
}
If you want to get it from ONE OF the VIEWS
//if Custom class
TabBarController *tabBar = (TabBarController *) self.tabBarController;
//if Custom class with Navigation Controller
TabBarController *tabBar = (TabBarController *) self.navigationController.tabBarController;
//if Not Subclassed
UITabBarController *tabBar = (UITabBarController *) self.tabBarController;
//if Not Subclassed with Navigation Controller
UITabBarController *tabBar = (UITabBarController *) self.navigationController.tabBarController;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With