ios - UITabBarController 的问题

标签 ios uitabbarcontroller

我正在使用 UITabBarController,

我选择 UITabBarController Tab 工作正常( View Controller 和选项卡图像已更改)。

但是当我尝试使用代码

AppDelegate.Tabctrl.selectedIndex = 2;

选择了右 View Controller ,但选项卡图像位于第 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

最佳答案

尝试使用此代码更改 Tabbar Controller 的图像:

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

关于ios - UITabBarController 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19899288/

相关文章:

ios - 具有相同选项卡 Controller 的多个 View Controller

ios - FileManager.default.removeItem 不删除文件

ios - 如何从其自己的 subview Controller 中解散模态 uitabbarcontroller

ios - 有没有办法获取 UITableViewCell 的 ID?

ios - shouldAutorotate 未在 View Controller 上调用

iphone - 如何创建全局导航堆栈?

swift - UITabBar isTranslucent 添加了一个额外的 UITabBar?

ios - UITableViewCells 之间的间距

ios - 如何在 iOS 自定义键盘中添加自定义通知?

ios - App Store Connect 提交审核错误 "ENTITY_UNPROCESSABLE.BETA_CONTRACT_MISSING"