Change tabbarController's tab programmatically with animation

后端 未结 3 1177
不知归路
不知归路 2021-01-14 16:11

I want to change tab through code with animation. Exact scenario is that there are 2 tabs with below hierarchy.

First tab
  - Navigation controller
    - Log         


        
3条回答
  •  遥遥无期
    2021-01-14 16:18

    try this

    - (void)logoutButtonPressed
    {
        // Go to root controller in navigation controller of first tab.
        [self.navigationController popToRootViewControllerAnimated:YES];
    
    
        NSArray *tabViewControllers = self.tabBarController.viewControllers;
    
    
        UIView * fromView = self.tabBarController.selectedViewController.view;
    
    
        UIView * toView = self.view;
    
        NSUInteger fromIndex = [tabViewControllers indexOfObject:self.tabBarController.selectedViewController];
    
    
        NSUInteger toIndex = [tabViewControllers indexOfObject:self];
    
        [UIView transitionFromView:fromView
                            toView:toView
                          duration:0.3
                           options: toIndex > fromIndex ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight
                        completion:^(BOOL finished) {
    
                                self.tabBarController.selectedIndex = 0;
    
                        }];
    
    
    }
    

提交回复
热议问题