Add a Tab Bar Controller Programmatically to current App Flow

前端 未结 2 1533
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 21:17

I want to add a tab Bar Controller to my current App Flow. Currently I have a page with a button which on clicking opens a new viewcontroller with a web view where the user logs

2条回答
  •  感动是毒
    2021-01-31 21:50

    The way I've done this in the past is to create a UITabBarController subclass that contains all of the tabBar creation code you have above.

    Then use your UINavigationController to push the tabBar subclass to the screen.

    Here's a sample of my UITabBarController subclass:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIViewController *view1 = [[UIViewController alloc] init];
        UIViewController *view2 = [[UIViewController alloc] init];
        UIViewController *view3 = [[UIViewController alloc] init];
    
        NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
        [tabViewControllers addObject:view1];
        [tabViewControllers addObject:view2];
        [tabViewControllers addObject:view3];
    
        [self setViewControllers:tabViewControllers];
        //can't set this until after its added to the tab bar
        view1.tabBarItem = 
          [[UITabBarItem alloc] initWithTitle:@"view1" 
                                        image:[UIImage imageNamed:@"view1"] 
                                          tag:1];
        view2.tabBarItem = 
          [[UITabBarItem alloc] initWithTitle:@"view2" 
                                        image:[UIImage imageNamed:@"view3"] 
                                          tag:2];
        view3.tabBarItem = 
          [[UITabBarItem alloc] initWithTitle:@"view3" 
                                        image:[UIImage imageNamed:@"view3"] 
                                          tag:3];      
    }
    

提交回复
热议问题