0

我想为我的应用程序制作一个自定义标签栏,并且我已经尝试将我的代码放在第一个视图控制器中并且它可以工作!但这是问题所在,我想声明并将我的代码放在 Appdelegate.m 中,但是当我运行它时,它只显示一个没有图像的黑色标签栏控制器。为了您的信息,我从情节提要中嵌入了我的标签栏控制器。

这是代码

    UIImage *selectedImage0   = [UIImage imageNamed:@"img1.png"];
    UIImage *unselectedImage0 = [UIImage imageNamed:@"img1hov.png"];

    UIImage *selectedImage1   = [UIImage imageNamed:@"img2.png"];
    UIImage *unselectedImage1 = [UIImage imageNamed:@"img2hov.png"];

    UIImage *selectedImage2   = [UIImage imageNamed:@"img3.png"];
    UIImage *unselectedImage2 = [UIImage imageNamed:@"img3hov.png"];

    UIImage *selectedImage3   = [UIImage imageNamed:@"img4.png"];
    UIImage *unselectedImage3 = [UIImage imageNamed:@"img4hov.png"];

    UIImage *selectedImage4   = [UIImage imageNamed:@"img5.png"];
    UIImage *unselectedImage4 = [UIImage imageNamed:@"img5hov.png"];



    UITabBar *tabBar = self.tabBarController.tabBar;
    UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
    UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
    UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
    UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
    UITabBarItem *item4 = [tabBar.items objectAtIndex:4];

    tabBar.backgroundImage = [UIImage imageNamed:@"back_menu.png"];


    [item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
    [item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
    [item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
    [item3 setFinishedSelectedImage:selectedImage3 withFinishedUnselectedImage:unselectedImage3];
    [item4 setFinishedSelectedImage:selectedImage4 withFinishedUnselectedImage:unselectedImage4];
4

2 回答 2

0

该代码对我来说看起来不错。如何在 appDelegate 中UIViewController添加?UITabBarController不是这样吗?

//appDelegate.h
@property (nonatomic, strong) SomeViewController *viewController;
@property (nonatomic, strong) UITabBarController *tabBarController;
//and synthesize those

//appDelegate.m
self.viewController = [[SomeViewController alloc]  initWithNibName:@"SomeViewController" bundle:nil];

UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers:[NSArray arrayWithObjects:self.viewController, nil];

您可以通过使用从任何地方访问self.tabBarControllerself.viewController驻留在 appDelegate 中((AppDelegate *)[UIApplication sharedApplication].delegate)

于 2013-02-21T05:04:30.463 回答