0

我正在使用 UITabBarController,

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

但是当我尝试使用代码时

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;

像这样输出

在此处输入图像描述

4

2 回答 2

1

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

 [[AppDelegate.Tabctrl.tabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:@"message12_64.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"message_112_64.png"]];
于 2013-11-11T05:23:03.897 回答
0

您正在尝试通过执行子视图来添加图像女巫不起作用,因为标签栏按钮是 Button 并且它们也具有图像属性,因此在此处添加您的图标图像,按照这个小教程并在开始之前阅读它,这样您就会明白

UITabBarController 以编程方式

用于设置 tabbaritem 图像与此一起使用

UITabBarItem *tabItem = [[[UITabBarItem tabBar] items] objectAtIndex:yourIndex];
[tabItem setTitle:@"theTitle"];
[tabItem setImage:[UIImage imageNamed:@"yourImage.png"]];
于 2013-11-11T05:29:17.027 回答