문제

I have an application which uses a tab bar. I want each tab to use a different tint color. Is there a way to have each tabBarItem be selected with a different color? Thanks!

도움이 되었습니까?

해결책

If you are using a UITabBarController, you can simply implement the following delegate and change the color for each tabs as you want.

In case you aren't using the UITabBarController, just add the correct delegate to your UITabBar.

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSUInteger indexOfTab = [[tabBar items] indexOfObject:item];
    UIColor *colorToApply;
    switch (indexOfTab) {
        case 0:
            colorToApply = [UIColor colorWithRed: 255.0/255.0 green: 64.0/255.0 blue: 98.0/255.0 alpha: 1.0];
            break;
        case 1:
            colorToApply = [UIColor colorWithRed: 254.0/255.0 green: 156.0/255.0 blue: 152.0/255.0 alpha: 1.0];
            break;
        case 2:
            colorToApply = [UIColor colorWithRed: 250.0/255.0 green: 205.0/255.0 blue: 171.0/255.0 alpha: 1.0];
            break;
        case 3:
            colorToApply = [UIColor colorWithRed: 200.0/255.0 green: 200.0/255.0 blue: 168.0/255.0 alpha: 1.0];
            break;
        case 4:
            colorToApply = [UIColor colorWithRed: 130.0/255.0 green: 175.0/255.0 blue: 155.0/255.0 alpha: 1.0];
            break;

        default:
            break;
    }
    tabBar.tintColor = colorToApply;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top