The tabBarController is not displayed

时光毁灭记忆、已成空白 提交于 2019-12-13 13:54:15

问题


i want to make tabbarcontroller programatically. The tabBarController is not displayed in the page. can anyone tell that whts going wrong.and can we make more than one tabbarcontroller in an application

ViewController.m
- (void)viewDidLoad
{
    report=[[UIViewController alloc]initWithNibName:@"ViewController" bundle:nil];
    View1 *template=[[View1 alloc]initWithNibName:@"View1" bundle:nil];
    View2 *acc=[[View2 alloc]initWithNibName:@"View2" bundle:nil];
    View3 *four=[[View3 alloc]initWithNibName:@"View3" bundle:nil];
    View4 *five=[[View4 alloc]initWithNibName:@"View4" bundle:nil];   

    nav1=[[UINavigationController alloc]initWithRootViewController:report];
    nav2=[[UINavigationController alloc]initWithRootViewController:template];
    nav3=[[UINavigationController alloc]initWithRootViewController: acc];
    nav4=[[UINavigationController alloc]initWithRootViewController:four];
     nav5=[[UINavigationController alloc]initWithRootViewController:five];

    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Title" image:[UIImage  imageNamed:@"singleicon.png"] tag:0];
    UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"Reports" image:[UIImage imageNamed:@"doubleicon.png"] tag:1];
    UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@" New " image:[UIImage imageNamed:@"clockicon.png"] tag:2];
    UITabBarItem *item3=[[UITabBarItem alloc]initWithTitle:@"four" image:[UIImage imageNamed:@"dependenticon.png"] tag:3];
    UITabBarItem *item4=[[UITabBarItem alloc]initWithTitle:@"five" image:[UIImage imageNamed:@"toolicon.png"] tag:4];

    nav1.tabBarItem = item;
    nav2.tabBarItem = item1;
    nav3.tabBarItem = item2;
    nav4.tabBarItem=item3;
    nav5.tabBarItem=item4;

    //[item1 setBadge:@"25"];
    self.tabBarController=[[UITabBarController alloc]init];
    [self.tabBarController setViewControllers:[NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nav5,nil]];

    self.report = self.tabBarController;
  // [self.report makeKeyAndVisible];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

回答1:


Use below code:

     self.tabBarController.viewControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nav4,nav5,nil]];
     self.window.rootViewController = self.tabBarController;

UPDATE:

also for Hide and Show the UITabBar then use bellow code ..

just put this methods in AppDelegate.m file and when you want to hide tabbar at that time just create AppDelegate object and call bellow hideTabBar method

- (void) hideTabBar:(UITabBarController *) tabbarcontroller {

    int height = 480;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

    for(UIView *view in tabbarcontroller.view.subviews) {
        if([view isKindOfClass:[UITabBar class]]) {
            [view setFrame:CGRectMake(view.frame.origin.x, height, view.frame.size.width, view.frame.size.height)];
        } 
        else {
            [view setFrame:CGRectMake(view.frame.origin.x,view.frame.origin.y, 320, 436)];
        }
    }
    [UIView commitAnimations];
}

- (void) showTabBar:(UITabBarController *) tabbarcontroller {

    int height = 436;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; 

    for(UIView *view in tabbarcontroller.view.subviews) {

        if([view isKindOfClass:[UITabBar class]]) {
            [view setFrame:CGRectMake(view.frame.origin.x, height, view.frame.size.width, view.frame.size.height)];            
        } 
        else {
            [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, height)];
        }
    }    

    [UIView commitAnimations];
}



回答2:


put all the coding in the action of the button and push the tabBarController like that:-

[self.navigationController pushViewController:tabBarController animated:YES]; 



回答3:


If you want to add UITabBarController programmatically, then you need to add your tabbarcontroller to your ViewController. you need to use this line,

[self.view addSubview:self.tabBarController.view];



回答4:


You need to add tabBarController in view you have missed the one line




回答5:


i just did this

[self.navigationController pushViewController:tabBarController animated:YES];


来源:https://stackoverflow.com/questions/13856184/the-tabbarcontroller-is-not-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!