ios - 更改未选择的标签栏项目的颜色

标签 ios objective-c uitabbarcontroller uitabbar uitabbaritem

我有一个标签栏 Controller ,我已经设法将选定的标签图像和标题设为黑色,将未选定的项目标题设为白色,但我无法将未选定的项目图像设为白色。

在我的标签栏 Controller 中: -(无效)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIColor *customColor = [UIColor colorWithRed:179.0/255.0f green:155.0/255.0f blue:107.0/255.0f alpha:1];

    [self.tabBar setBarTintColor:customColor];

    [self.tabBar setSelectedImageTintColor:[UIColor blackColor]];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor blackColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];

    UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];
    UITabBarItem *item1 = [self.tabBar.items objectAtIndex:1];
    UITabBarItem *item2 = [self.tabBar.items objectAtIndex:2];

    item0.image = [[UIImage imageNamed:@"home_unselected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item0.selectedImage = [UIImage imageNamed:@"home_tab"];
    item1.image = [[UIImage imageNamed:@"contact_unselected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item1.selectedImage = [UIImage imageNamed:@"contact_tab"];
    item2.image = [[UIImage imageNamed:@"about_unselected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    item2.selectedImage = [UIImage imageNamed:@"about_tab"];
}

enter image description here

最佳答案

我是这样工作的:

标签栏 Controller :

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UIColor *customColor = [UIColor colorWithRed:179.0/255.0f green:155.0/255.0f blue:107.0/255.0f alpha:1];

    [self.tabBar setBarTintColor:customColor];
    [self.tabBar setTintColor:[UIColor blackColor]];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor blackColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
}

在标签栏上的三个 View Controller 中的每一个中:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.title = NSLocalizedString(@"Home", @"Home");
        self.tabBarItem.image = [[UIImage imageNamed:@"home_unselected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        self.tabBarItem.selectedImage = [UIImage imageNamed:@"home_tab"];
    }
    return self;
}

关于ios - 更改未选择的标签栏项目的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23875285/

相关文章:

ios - 在 Popover 中显示 ImagePicker - ipad

iOS - 如何测量线程唤醒?

objective-c - 在 obj-c 对象而不是 C 结构上调用 IOUSBDeviceInterface 函数

objective-c - 以编程方式将 UIView 附加到 UIViewController?

ios - 如何防止 UITabBar 展开转场?

ios - 如何将 NSstring 转换为 NSerror 对象类型

ios - 在drawRect()方法中绘制CoreGraphics线时遇到问题

ios - ios中sendEvent和Send Action的区别

iphone - Objective-C 如何有选择地禁用用户交互

ios - 访问另一个 View 的元素(iOS - 标签栏 Controller )