UITabBarの選択色をデフォルトの青から赤に変更する必要があります。これをどのように行いますか。
13 に答える
IOS5では、UITabBarにselectedImageTintColorプロパティがあり、必要な処理を実行します。
iOS 7 では、単純に tintColor です。これを実現する 1 つの方法は、UITabBarViewController をサブクラス化し、ストーリーボードにカスタム クラスを設定し、サブクラス化されたviewDidLoad
tabBarVC のメソッドにこれを追加することです。
[[self tabBar] setTintColor:[UIColor redColor]];
とても簡単です
UITabBarControllerのカスタムクラスを作成し、-(void)viewDidLoad
メソッドに次の行を追加します。
[[self tabBar] setSelectedImageTintColor:[UIColor greenColor]];
UITextAttributeTextColor は iOS 7 で廃止されたため、以下を使用する必要があります。
[UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor]} forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor purpleColor]} forState:UIControlStateSelected];
TabBar の Interface Builder で次のプロパティを変更するだけです。
明らかに私の場合は白です。
UITabBarItem の選択されたテキストの色を設定する方法を探していたところ、UIAppearance プロトコルを使用して非常に単純な方法を見つけました。
[UITabBarItem.appearance setTitleTextAttributes:@{
UITextAttributeTextColor : [UIColor greenColor] } forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:@{
UITextAttributeTextColor : [UIColor purpleColor] } forState:UIControlStateSelected];
色が汚いのは許してください!
iOS 5.0 ではこの問題が修正されていますが、ソリューションは NDA の下にあります。やりたいことを簡単に行う方法については、ドキュメントで UITabBar を調べてください。