I want to change tab through code with animation. Exact scenario is that there are 2 tabs with below hierarchy.
First tab
- Navigation controller
- Login controller
- Some other controller
Second tab
- Navigation controller
- Screen with Logout button
Now, if user presses logout, I need to display login screen. For that I need to switch tab to FirstTab
and then popToRootViewController.
So what I am doing is on logout button press I send NSNotification
to LoginController
which in turn executes below method.
- (void)logoutButtonPressed
{
// Go to root controller in navigation controller of first tab.
[self.navigationController popToRootViewControllerAnimated:YES];
// Change tab to "First tab". This happens sharply without animation.
// I want to animate this change.
self.tabBarController.selectedIndex = 0;
}
I tried below method to animate. But this animates only when tab is changed by user but not when changed through code.
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSArray *tabViewControllers = tabBarController.viewControllers;
UIView * fromView = tabBarController.selectedViewController.view;
UIView * toView = viewController.view;
if (fromView == toView)
return false;
NSUInteger fromIndex = [tabViewControllers indexOfObject:tabBarController.selectedViewController];
NSUInteger toIndex = [tabViewControllers indexOfObject:viewController];
[UIView transitionFromView:fromView
toView:toView
duration:0.3
options: toIndex > fromIndex ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight
completion:^(BOOL finished) {
if (finished) {
tabBarController.selectedIndex = toIndex;
}
}];
return true;
}
I'm Sorry Geek, I was disconnected on weekend.
The solution that I took in my app was make a method which call to the
popToRootViewControllerAnimated:
and then send aperformSelector:
message toself
passing that created method as a selector:Maybe it is not the best and well performed solution, but it's an option because do the work. It will select the tab and after a 1.5 second delay, the animation will take place. Hope it helps.
Below is the code I used to animate screens with slide in-out effect. Found on SO question.
try this