簡體   English   中英

更改未選擇的選項卡欄項目的顏色

[英]Change Color of Unselected Tab Bar Item

我有一個標簽欄控制器,並且設法將選中的標簽圖像和標題設置為黑色,將未選中的項目標題設置為白色,但是無法將未選中的項目圖像設置為白色。

在我的標簽欄控制器中:-(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 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"];
}

在此處輸入圖片說明

我讓它像這樣工作:

標簽欄控制器:

- (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];
}

在選項卡欄上的三個視圖控制器中的每一個中:

- (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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM