0

我正在尝试设置 UITabBarItem 的图标,但它不起作用。顺便说一句,我正在为这个项目使用 Xcode 5 Beta。

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    {
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
        UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
        [[UITabBar appearance] setBackgroundImage:tabBarBackground];
        [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];
        [[UITabBar appearance] setTintColor:[UIColor whiteColor]];
    }
    return YES;
}

在此处输入图像描述

我试图在选中和未选中时使图标变为白色,但在未选中状态下它们保持灰色。


编辑

我试过这样做,但现在我收到错误“未使用的表达式结果”

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
        UITabBar *tabBar = tabBarController.tabBar;
        UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
        tabBarItem1.title = @"Home";
        UIImage *graph = [UIImage imageNamed:@"graph.png"];
        [tabBarItem1 initWithTitle:(NSString *)@"HELLO" image:(UIImage *)graph selectedImage:(UIImage *)graph];
4

1 回答 1

0

tintColor设置背景的色调颜色,如果您使用自定义背景图像,则看不到这一点。如果您不想使用系统默认渐变,则必须使用setFinishedSelectedImage:withFinishedUnselectedImage:并为它​​们设置白色图像。

像这样:

UIImage *selectedImage = ...
UIImage *unselectedImage = selectedImage;
[tabBarItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];

如果您在 Storyboard 中设置这些图像,您可以迭代现有的 UITabBarItems 并更改它们的图像。

把这样的东西放入application:didFinishLaunchingWithOptions:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
NSAssert([tabBarController isKindOfClass:[UITabBarController class]], @"self.window.rootViewController must be a tabBarController");
for (UIViewController *viewController in tabBarController.viewControllers) {
    UITabBarItem *tabBarItem = viewController.tabBarItem;
    UIImage *tabImage = tabBarItem.image;
    [tabBarItem setFinishedSelectedImage:tabImage withFinishedUnselectedImage:tabImage];
}
于 2013-08-19T22:18:34.447 回答