27

iOS 7 で UITabBar と UITabBarItems のテキストとアイコンの色を変更するにはどうすればよいですか? デフォルトの灰色のテキストは、選択されていないタブバーの項目では薄くて読みにくいように見えます。

4

11 に答える 11

70

これには、次の 2 つのことを行う必要があります。

1) TabBar 自体をカスタマイズする場合は、tabBarController の barTintColor を設定する必要があります。

    // this will generate a black tab bar
    tabBarController.tabBar.barTintColor = [UIColor blackColor];

    // this will give selected icons and text your apps tint color
    tabBarController.tabBar.tintColor = appTintColor;  // appTintColor is a UIColor *

2) オーバーライドする各状態の tabBarItem テキストの外観を設定します。

[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
                                                    NSForegroundColorAttributeName : appTintColor
                                                    } forState:UIControlStateSelected];


// doing this results in an easier to read unselected state then the default iOS 7 one
[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
                                                    NSForegroundColorAttributeName : [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1]
                                                    } forState:UIControlStateNormal];
于 2013-09-11T07:09:22.603 に答える
44

これは私にとってはうまくいき、タブバーのアクティブでないアイテムに色を付けました

UITabBarItem *item = [self.tabBar.items objectAtIndex:1];

// ここでは、そのままレンダリングされるため、必要な色のアイコンを使用する必要があります

item.image = [[UIImage imageNamed:@"unselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

// このアイコンは選択されたタブに使用され、で定義されているように着色されます

self.tabBar.tintColor
item.selectedImage = [UIImage imageNamed:@"selected.png"];
于 2013-10-29T17:48:29.367 に答える
7

タブごとに異なる色の 2 つの画像を作成せずに、永続的なテキストの色 (選択/選択解除) と画像の色 (選択/選択解除) について iOS 8 でテスト済み:

テキストの色:

[[UITabBar appearance] setTintColor: selectedTabColor ];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       **yourFont**, NSFontAttributeName,
                                                       ** selectedTabColor**, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateNormal];

    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       **yourFont**, NSFontAttributeName,
                                                       **selectedTabColor**, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateSelected];

画像の色: (元の画像が選択されていない状態で表示したい色であると仮定します)

UITabBarControllerサブクラス-awakeFromNib :

    for (int i =0; i<self.viewControllers.count; i++)
    {
        UITabBarItem *tab = [self.tabBar.items objectAtIndex:i];
        tab.image = [tab.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
    }

クレジット: インターネット全体とスタック オーバーフロー XD

于 2015-09-04T16:43:36.327 に答える
7

タブバーのテキストの色を変更するコードフリーの方法:

iOS 10 のみを使用している場合は、タブ バーで画像の色合いを変更できます。

ここに画像の説明を入力

iOS 9 以前もサポートしている場合は、各タブ バー項目のユーザー定義ランタイム属性に tintColor も追加する必要があります。

ここに画像の説明を入力

アイコンの色も変更したい場合は、正しいカラー画像が assest フォルダーにあることを確認し、Render as to Original Image を変更してください。

ここに画像の説明を入力

于 2016-10-28T16:54:29.627 に答える
2
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor whiteColor], UITextAttributeTextColor,
                                                           nil] 
于 2014-02-15T09:03:08.963 に答える
0

self.tabBarController.tabBar.barStyle = UIBarStyleBlack;タブバーを黒くするために使用します

于 2013-09-25T08:22:59.927 に答える
0

今からiOS10使える

@property (nonatomic, readwrite, copy, nullable) UIColor *unselectedItemTintColor

TabBarItem選択されていない状態での画像とテキストのデフォルトの色を変更します。

tintColorしたがって、プロパティとプロパティのペアにより、unselectedItemTintColorアイテムの色を完全に制御できます。

于 2019-04-14T16:57:46.540 に答える