iOS 7 で UITabBar と UITabBarItems のテキストとアイコンの色を変更するにはどうすればよいですか? デフォルトの灰色のテキストは、選択されていないタブバーの項目では薄くて読みにくいように見えます。
11 に答える
これには、次の 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];
これは私にとってはうまくいき、タブバーのアクティブでないアイテムに色を付けました
UITabBarItem *item = [self.tabBar.items objectAtIndex:1];
// ここでは、そのままレンダリングされるため、必要な色のアイコンを使用する必要があります
item.image = [[UIImage imageNamed:@"unselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// このアイコンは選択されたタブに使用され、で定義されているように着色されます
self.tabBar.tintColor
item.selectedImage = [UIImage imageNamed:@"selected.png"];
タブごとに異なる色の 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
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
nil]
self.tabBarController.tabBar.barStyle = UIBarStyleBlack;
タブバーを黒くするために使用します
今からiOS10
使える
@property (nonatomic, readwrite, copy, nullable) UIColor *unselectedItemTintColor
TabBarItem
選択されていない状態での画像とテキストのデフォルトの色を変更します。
tintColor
したがって、プロパティとプロパティのペアにより、unselectedItemTintColor
アイテムの色を完全に制御できます。