简体   繁体   English

如何根据TabBar选择的索引设置色调颜色?

[英]How to set tint color according to TabBar selected index?

I want to change the tintColor property according to tabbar's selected index. 我想根据tabbar的选定索引更改tintColor属性。 The code I'm using now is not working, all the tabbar views have this code: 我现在使用的代码不起作用,所有tabbar视图都有以下代码:

- (void)viewDidAppear:(BOOL)animated{

    switch (self.tabBarController.selectedIndex) {
        case 0:
            self.tabBarController.tintColor = [UIColor colorWithRed:147/255 green:22/255 blue:0/255 alpha:1.0];
            break;

        case 1:
            self.tabBarController.tintColor = [UIColor whiteColor];
            break;

        case 2:
            self.tabBarController.tintColor = [UIColor greenColor];
            break;

        default:
            break;
    }
}

You don't need to do that; 你不需要这样做; instead, put this in the viewWillAppear: method of your contained controllers: 相反,将它放在包含控制器的viewWillAppear:方法中:

Controller 1: 控制器1:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.tintColor = [UIColor blueColor];
}

Controller 2: 控制器2:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.tintColor = [UIColor yellowColor];
}

This assumes iOS5+. 这假设是iOS5 +。 Hope this helps. 希望这可以帮助。

Instead of 代替

self.tabBarController.tintColor

Use: 采用:

self.tabBarController.tabBar.tintColor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM