UITabBarController问题

时间:2013-11-11 05:14:23

标签: ios uitabbarcontroller

我正在使用UITabBarController,

我选择UITabBarController Tab工作正常(视图控制器和Tab图像已更改)。

但是当我尝试使用代码时

AppDelegate.Tabctrl.selectedIndex = 2;

右侧视图控制器被选中但是第0个标签本身的标签图像,我尝试使用TabBarController的手动图像。这是代码。

-(void) LoadTabbarcontrol
{
    UIStoryboard *board  = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 

    AppDelegate.Tabctrl = [board instantiateViewControllerWithIdentifier:@"TabbarCtrlId"];

    AppDelegate.Tabctrl.delegate = self;

    UIWindow *window = AppDelegate.window;
    [window addSubview:AppDelegate.Tabctrl.view];
    [window makeKeyAndVisible];

    imgTab = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320.0,44.0)];
    imgTab.image=[UIImage imageNamed:@"Tabbar_bg.png"];
    [AppDelegate.Tabctrl.tabBar addSubview:imgTab];

    imgHome=[[UIImageView alloc]initWithFrame:CGRectMake(4.0,1.0,60.0,44.0)];
    imgHome.image=[UIImage imageNamed:@"Home-Select.png"];
    [imgTab addSubview:imgHome];

    imgMyTc=[[UIImageView alloc]initWithFrame:CGRectMake(67.0,1.0,60.0,44.0)];
    imgMyTc.image=[UIImage imageNamed:@"MyTc.png"];
    [imgTab addSubview:imgMyTc];

    imgBrowse=[[UIImageView alloc]initWithFrame:CGRectMake(130.0,1.0,60.0,44.0)];
    imgBrowse.image=[UIImage imageNamed:@"Browse.png"];
    [imgTab addSubview:imgBrowse];

    imgInstore=[[UIImageView alloc]initWithFrame:CGRectMake(193.0,1.0,60.0,44.0)];
    imgInstore.image=[UIImage imageNamed:@"Instore.png"];
    [imgTab addSubview:imgInstore];

    imgMore=[[UIImageView alloc]initWithFrame:CGRectMake(256.0,1.0,60.0,44.0)];
    imgMore.image=[UIImage imageNamed:@"More.png"];
    [imgTab addSubview:imgMore];

    AppDelegate.Tabctrl.view.hidden = NO;
    AppDelegate.Tabctrl.selectedIndex = 2;
}


    - (void)tabBarController:(UITabBarController *)tabBarControllers didSelectViewController:(UIViewController *)viewController
    {
        if (tabBarControllers.selectedIndex == 0)
        {
            imgHome.image=[UIImage imageNamed:@"Home-Select.png"];
            imgMyTc.image=[UIImage imageNamed:@"MyTc.png"];
            imgBrowse.image=[UIImage imageNamed:@"Browse.png"];
            imgInstore.image=[UIImage imageNamed:@"Instore.png"];
            imgMore.image=[UIImage imageNamed:@"More.png"];
        }
        else if (tabBarControllers.selectedIndex == 1)
        {
            imgHome.image=[UIImage imageNamed:@"Home.png"];
            imgMyTc.image=[UIImage imageNamed:@"MyTc-Select.png"];
            imgBrowse.image=[UIImage imageNamed:@"Browse.png"];
            imgInstore.image=[UIImage imageNamed:@"Instore.png"];
            imgMore.image=[UIImage imageNamed:@"More.png"];
        }
        else if (tabBarControllers.selectedIndex == 2)
        {
            imgHome.image=[UIImage imageNamed:@"Home.png"];
            imgMyTc.image=[UIImage imageNamed:@"MyTc.png"];
            imgBrowse.image=[UIImage imageNamed:@"Browse-Select.png"];
            imgInstore.image=[UIImage imageNamed:@"Instore.png"];
            imgMore.image=[UIImage imageNamed:@"More.png"];
        }
        else if (tabBarControllers.selectedIndex == 3)
        {
            imgHome.image=[UIImage imageNamed:@"Home.png"];
            imgMyTc.image=[UIImage imageNamed:@"MyTc.png"];
            imgBrowse.image=[UIImage imageNamed:@"Browse.png"];
            imgInstore.image=[UIImage imageNamed:@"Instore-Select.png"];
            imgMore.image=[UIImage imageNamed:@"More.png"];
        }
        else if (tabBarControllers.selectedIndex == 4)
        {
            imgHome.image=[UIImage imageNamed:@"Home.png"];
            imgMyTc.image=[UIImage imageNamed:@"MyTc.png"];
            imgBrowse.image=[UIImage imageNamed:@"Browse.png"];
            imgInstore.image=[UIImage imageNamed:@"Instore.png"];
            imgMore.image=[UIImage imageNamed:@"More-Select.png"];
        }
    } 

任何人都可以帮我吗?

如果我提供

,请参阅示例屏幕
AppDelegate.Tabctrl.selectedIndex = 2;

像这样输出

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试使用此代码更改Tabbar控制器的图像:

 [[AppDelegate.Tabctrl.tabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:@"message12_64.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"message_112_64.png"]];

答案 1 :(得分:0)

你试图通过做子视图添加图像因为tabbar按钮是Button而且它们也具有图像属性,所以在那里添加你的图标图像,按照这个小教程在开始之前阅读它以便你理解

UITabBarController Programmatically

用于设置tabbaritem图像的

使用此

UITabBarItem *tabItem = [[[UITabBarItem tabBar] items] objectAtIndex:yourIndex];
[tabItem setTitle:@"theTitle"];
[tabItem setImage:[UIImage imageNamed:@"yourImage.png"]];