87

UINavigationBarとUISearchBarの両方にtintColorプロパティがあり、これらのアイテムの両方の色合いの色を変更できます(驚くべきことに、私は知っています)。アプリケーションのUITabBarにも同じことをしたいのですが、デフォルトの黒色から変更する方法を見つけました。何か案は?

4

18 に答える 18

105

iOS 5 では、ほとんどの UI 要素の外観をカスタマイズするための新しい外観メソッドがいくつか追加されました。

外観プロキシを使用して、アプリ内の UITabBar のすべてのインスタンスを対象にすることができます。

iOS 5 + 6 の場合:

[[UITabBar appearance] setTintColor:[UIColor redColor]];

iOS 7 以降の場合は、次を使用してください。

[[UITabBar appearance] setBarTintColor:[UIColor redColor]];

外観プロキシを使用すると、アプリ全体のタブ バー インスタンスが変更されます。特定のインスタンスに対して、そのクラスの新しいプロパティの 1 つを使用します。

UIColor *tintColor; // iOS 5+6
UIColor *barTintColor; // iOS 7+
UIColor *selectedImageTintColor;
UIImage *backgroundImage;
UIImage *selectionIndicatorImage;
于 2011-12-19T21:18:08.930 に答える
47

UITabBarControllerをサブクラス化し、プライベートクラスを使用することで、これを機能させることができました。

@interface UITabBarController (private)
- (UITabBar *)tabBar;
@end

@implementation CustomUITabBarController


- (void)viewDidLoad {
    [super viewDidLoad];

    CGRect frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, 48);
    UIView *v = [[UIView alloc] initWithFrame:frame];
    [v setBackgroundColor:kMainColor];
    [v setAlpha:0.5];
    [[self tabBar] addSubview:v];
    [v release];

}
@end
于 2009-03-14T17:17:56.867 に答える
34

最終回答に補足があります。基本的なスキームは正しいですが、部分的に透明な色を使用するというトリックは改善することができます。デフォルトのグラデーションが透けて見えるようにするためだけだと思います。また、少なくとも OS 3 では、TabBar の高さは 48 ではなく 49 ピクセルです。

したがって、グラデーションのある適切な 1 x 49 画像がある場合は、これが使用する必要がある viewDidLoad のバージョンです。

- (void)viewDidLoad {

    [super viewDidLoad]; 

    CGRect frame = CGRectMake(0, 0, 480, 49);
    UIView *v = [[UIView alloc] initWithFrame:frame];
    UIImage *i = [UIImage imageNamed:@"GO-21-TabBarColorx49.png"];
    UIColor *c = [[UIColor alloc] initWithPatternImage:i];
    v.backgroundColor = c;
    [c release];
    [[self tabBar] addSubview:v];
    [v release];

}
于 2009-12-30T20:51:34.910 に答える
27

addSubview を使用するだけでは、ボタンをクリックできなくなるため、代わりに

[[self tabBar] addSubview:v];

使用する:

[[self tabBar] insertSubview:v atIndex:0];
于 2009-10-05T10:45:43.610 に答える
7

以下は、これに対する完璧なソリューションです。これは、iOS5 と iOS4 で問題なく動作します。

//---- For providing background image to tabbar
UITabBar *tabBar = [tabBarController tabBar]; 

if ([tabBar respondsToSelector:@selector(setBackgroundImage:)]) {
    // ios 5 code here
    [tabBar setBackgroundImage:[UIImage imageNamed:@"image.png"]];
} 
else {
    // ios 4 code here
    CGRect frame = CGRectMake(0, 0, 480, 49);
    UIView *tabbg_view = [[UIView alloc] initWithFrame:frame];
    UIImage *tabbag_image = [UIImage imageNamed:@"image.png"];
    UIColor *tabbg_color = [[UIColor alloc] initWithPatternImage:tabbag_image];
    tabbg_view.backgroundColor = tabbg_color;
    [tabBar insertSubview:tabbg_view atIndex:0];
}
于 2012-02-03T05:21:00.383 に答える
7

これを行う簡単な方法はありません。基本的には、UITabBar をサブクラス化し、カスタム描画を実装して、必要なことを行う必要があります。効果を得るにはかなりの作業ですが、それだけの価値があるかもしれません。Apple にバグを報告して、将来の iPhone SDK に追加してもらうことをお勧めします。

于 2009-02-20T20:11:21.487 に答える
5

私にとっては、タブバーの色を次のように変更するのは非常に簡単です:-

[self.TabBarController.tabBar setTintColor:[UIColor colorWithRed:0.1294 green:0.5686 blue:0.8353 alpha:1.0]];


[self.TabBarController.tabBar setTintColor:[UIColor "YOUR COLOR"];

これを試して!!!

于 2013-03-01T09:34:44.547 に答える
5

[[self tabBar] insertSubview:v atIndex:0]; 私にとって完璧に機能します。

于 2010-06-03T18:12:09.223 に答える
3
 [[UITabBar appearance] setTintColor:[UIColor redColor]];
 [[UITabBar appearance] setBarTintColor:[UIColor yellowColor]];
于 2014-10-31T01:16:24.517 に答える
2

背景色のみ

Tabbarcontroller.tabBar.barTintColor=[UIColor redcolour];

またはこれをApp Delegateで

[[UITabBar appearance] setBackgroundColor:[UIColor blackColor]];

タブバーの非選択アイコンの色を変更する

iOS 10 の場合:

// this code need to be placed on home page of tabbar    
for(UITabBarItem *item in self.tabBarController.tabBar.items) {
    item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

iOS 10 以上:

// this need to be in appdelegate didFinishLaunchingWithOptions
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blackColor]];
于 2016-11-03T13:27:12.650 に答える
1
[v setBackgroundColor ColorwithRed: Green: Blue: ];
于 2009-06-08T06:45:17.183 に答える
1

既存の回答にはいくつかの良いアイデアがあり、多くはわずかに異なる動作をします。選択するものは、ターゲットとするデバイスと達成しようとしている外観の種類によっても異なります. UITabBar外観のカスタマイズに関しては直感的でないことで有名ですが、役立つかもしれないいくつかのトリックを次に示します。

1)。よりフラットな外観にするために光沢のあるオーバーレイを取り除きたい場合は、次のようにします。

tabBar.backgroundColor = [UIColor darkGrayColor]; // this will be your background
[tabBar.subviews[0] removeFromSuperview]; // this gets rid of gloss

2)。カスタム イメージを tabBar ボタンに設定するには、次のようにします。

for (UITabBarItem *item in tabBar.items){
    [item setFinishedSelectedImage:selected withFinishedUnselectedImage:unselected];
    [item setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)];
}

selectedunselectedは選択しUIImageたオブジェクトです。それらを単色にしたい場合、私が見つけた最も簡単な解決策は、目的の を作成しUIView、QuartzCore をbackgroundColor使用してそれを にレンダリングすることです。ビューのコンテンツを取得するためUIImageに、カテゴリで次のメソッドを使用します。UIViewUIImage

- (UIImage *)getImage {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [[UIScreen mainScreen]scale]);
    [[self layer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

3) 最後に、ボタンのタイトルのスタイルをカスタマイズできます。行う:

for (UITabBarItem *item in tabBar.items){
    [item setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                [UIColor redColor], UITextAttributeTextColor,
                [UIColor whiteColor], UITextAttributeTextShadowColor,
                [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
                [UIFont boldSystemFontOfSize:18], UITextAttributeFont,
            nil] forState:UIControlStateNormal];
}

これにより、いくつかの調整を行うことができますが、それでもかなり制限されています。特に、ボタン内でテキストを配置する場所を自由に変更することはできません。また、選択されているボタンと選択されていないボタンの色を変えることはできません。より具体的なテキスト レイアウトを行いたい場合は、クリアに設定して、パート (2)のと画像UITextAttributeTextColorにテキストを追加します。selectedunselected

于 2013-02-20T21:23:41.183 に答える
0

もう 1 つの解決策 (ハック) は、tabBarController のアルファを 0.01 に設定して、実質的に見えなくてもクリックできるようにすることです。次に、アルファ化された tabBarCONtroller の下にあるカスタム タブバー イメージを使用して、MainWindow nib の下部に ImageView コントロールを設定します。次に、タブバーコントローラーがビューを切り替えるときに、画像を交換したり、色を変更したり、ハイライトしたりします。

ただし、「...詳細」およびカスタマイズ機能は失われます。

于 2011-08-03T11:09:13.253 に答える
0

こんにちは、iOS SDK 4 を使用しています。2 行のコードでこの問題を解決できました。次のようになります。

tBar.backgroundColor = [UIColor clearColor];
tBar.backgroundImage = [UIImage imageNamed:@"your-png-image.png"];

お役に立てれば!

于 2011-12-22T17:55:18.523 に答える