无法将第二个选项卡栏添加到选项卡控制器

时间:2012-12-21 08:42:43

标签: ios5 uitabbarcontroller uitabbar

我有两个标签栏,以便像问题Sliding UITabBarItems in UITabBarController

中显示的那样生效

我尝试使用最后一个索引的标签栏项目显示另一个标签栏,而隐藏当前标签栏,而不是箭头。我在viewDidLoad中以编程方式添加第二个标签栏。问题是,当点击最后一个标签栏项时,我的第二个标签栏没有显示出来。我所做的是:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tabControler.view.frame = self.view.frame;
    self.tabControler.delegate = self;
    self.secondTabBarSelected = NO;

    self.secondTabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 431, 320, 49)];
    self.secondTabBar.hidden = YES;
    self.secondTabBar.delegate = self;
    self.secondTabBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [self.tabControler.view addSubview:secondTabBar];

    [self.view addSubview:tabControler.view];
    NSLog(@"children of tabcon: %@",[tabControler.view subviews]); //Here second tab bar added with correct frame
}

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (!self.secondTabBarSelected) {
        NSLog(@"first tab bar");
        if([[tabBarController viewControllers] indexOfObject:viewController] == 3)
        {
            self.firstTabBar.hidden = YES;
            self.secondTabBar.hidden = NO;

        }
    }
}

1 个答案:

答案 0 :(得分:0)

我发现我的标签栏不可见,因为我在nib文件中显示时显式分配了它。相反,我只是设置框架。我更正的代码是:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tabControler.view.frame = self.view.frame;
    self.tabControler.delegate = self;
    self.secondTabBarSelected = NO;

    self.secondTabBar.frame = self.firstTabBar.frame;
    self.secondTabBar.hidden = YES;
    self.secondTabBar.delegate = self;
    NSLog(@"viewdidload frame: %@",secondTabBar.frame);

    self.secondTabBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [self.tabControler.view addSubview:self.secondTabBar];
    NSLog(@"viewdidload frame: %@",secondTabBar.frame);


    [self.view addSubview:tabControler.view];
}

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

        NSLog(@"first tab bar - controller");
        if([[tabBarController viewControllers] indexOfObject:viewController] == 3)
        {
            self.firstTabBar.hidden = YES;
            self.secondTabBar.hidden = NO;
            self.secondTabBarSelected  = YES;
        }

}