変化する色合い/バックグラウンドカラーでペイUITabBar
-
05-09-2019 - |
質問
のUINavigationBarとUISearchBar互いのtintColor物件を変更することができます。薄い色(ねん)のものです。をしたいのと同じようなことをUITabBar私のアプリケーションですが、いつ今の方からの変更をデフォルトの黒色です。そのアイデア?
解決
私はそれが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
他のヒント
のiOS 5は、ほとんどのUI要素の外観をカスタマイズするためのいくつかの新しい外観のメソッドを追加しています。
あなたは外観プロキシを使用して、アプリでUITabBarのすべてのインスタンスをターゲットにすることができます。
のiOS 5 + 6の場合:
[[UITabBar appearance] setTintColor:[UIColor redColor]];
上記のiOS 7とは、以下をご使用くださいます:
[[UITabBar appearance] setBarTintColor:[UIColor redColor]];
外観プロキシを使用すると、アプリ全体で任意のタブバーのインスタンスを変更します。特定のインスタンスの場合は、そのクラスに新しいプロパティのいずれかを使用します:
UIColor *tintColor; // iOS 5+6
UIColor *barTintColor; // iOS 7+
UIColor *selectedImageTintColor;
UIImage *backgroundImage;
UIImage *selectionIndicatorImage;
私は最終的な答えに補遺を持っています。本質的なスキームが正しいが、部分的に透明な色を使用してのトリックを大幅に改善することができます。私はそれだけ透けて見えるようにデフォルトのグラデーションをさせるためだと仮定します。ああ、また、TabBarの高さは、少なくともOS 3において、49個のピクセルよりもむしろ48である。
あなたは勾配で、適切な1×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];
}
あなたがちょうどそう代わりに、クリック可能に失うことになるaddSubviewあなたのボタンを使用すると、
[[self tabBar] addSubview:v];
使用:
[[self tabBar] insertSubview:v atIndex:0];
これを実行する簡単な方法はありません、あなたは基本的にUITabBarをサブクラス化し、あなたがやりたいカスタム描画を実装する必要があります。これは、効果のための仕事のかなりあるが、それはそれ価値があるかもしれません。私はそれが将来のiPhone SDKに追加されますし、アップルにバグを提出するお勧めします。
以下は、このための完璧なソリューションです。これは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];
}
iOSの7の場合:
[[UITabBar appearance] setBarTintColor:[UIColor colorWithRed:(38.0/255.0) green:(38.0/255.0) blue:(38.0/255.0) alpha:1.0]];
私はまたあなたの視覚的な欲望に応じて、最初の設定をお勧めします
[[UITabBar appearance] setBarStyle:UIBarStyleBlack];
バーのスタイルは、あなたのビューの内容とあなたのタブバーの間に微妙な区切りを置きます。
[[self tabBar] insertSubview:v atIndex:0];
私のために完璧に動作します。
私のためにそのようタブバーの色を変更することは非常にシンプル: -
[self.TabBarController.tabBar setTintColor:[UIColor colorWithRed:0.1294 green:0.5686 blue:0.8353 alpha:1.0]];
[self.TabBarController.tabBar setTintColor:[UIColor "YOUR COLOR"];
これを試してみてください!!!
[[UITabBar appearance] setTintColor:[UIColor redColor]];
[[UITabBar appearance] setBarTintColor:[UIColor yellowColor]];
ちょうど背景色のための の
Tabbarcontroller.tabBar.barTintColor=[UIColor redcolour];
またはアプリケーションの委任でこの
[[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]];
があり、いくつかの良いアイデアは少し異なり、既存の答え、多くの仕事であり、あなたが選択したことも、あなたはどのターゲットデバイスに依存し、あなたが達成することを目指している表情の種類。それは、その外観をカスタマイズするに来たときにUITabBar
は悪名高い非直観的であるが、ここでは役立つかもしれないいくつかのより多くのトリックがあります:
1)。あなたがよりフラットな外観のために光沢のあるオーバーレイを取り除くために探している場合は行います:
tabBar.backgroundColor = [UIColor darkGrayColor]; // this will be your background
[tabBar.subviews[0] removeFromSuperview]; // this gets rid of gloss
2)。以下のような何かをタブバーのボタンにカスタムイメージを設定するには:
for (UITabBarItem *item in tabBar.items){
[item setFinishedSelectedImage:selected withFinishedUnselectedImage:unselected];
[item setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)];
}
どこselected
とunselected
は、お好みのUIImage
オブジェクトです。あなたは彼らがフラットカラーになりたい場合は、私が見つけた最も簡単な解決策が望まUIView
でbackgroundColor
を作成してから、ちょうどQuartzCoreの助けを借りてUIImage
にそれをレンダリングすることです。私は、ビューの内容をUIView
を取得するためにUIImage
上のカテゴリに次のメソッドを使用します:
- (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
とselected
画像にテキストを追加するためにunselected
を設定します。
[v setBackgroundColor ColorwithRed: Green: Blue: ];
別の解決策は、それが事実上不可視依然としてクリック可能であるように0.01にtabBarControllerにアルファを設定することです。その後alpha'ed tabBarCOntrollerの下にカスタムタブバーの画像とメインウィンドウペン先の底面にImageViewのコントロールを設定します。次いで、画像を入れ替える色を変更したりtabbarcontroller、ビューを切り替えるときhightlight。
ただし、 '...もっと' とカスタマイズ機能を失います。
こんにちはのiOS SDK 4を使用して、私は、コードのわずか2行で、この問題を解決することができたし、それはだています。
このように書きますtBar.backgroundColor = [UIColor clearColor];
tBar.backgroundImage = [UIImage imageNamed:@"your-png-image.png"];
この情報がお役に立てば幸い!
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];
}
迅速3.0答え: (Vaibhav Gaikwad)
変色の選択を解除アイコンのtabbar:
if #available(iOS 10.0, *) {
UITabBar.appearance().unselectedItemTintColor = UIColor.white
} else {
// Fallback on earlier versions
for item in self.tabBar.items! {
item.image = item.image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
}
}
の変更テキストの色:
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.red, for: .selected)
あなたのAppDelegate
から外観を使用してスウィフト3は、以下の操作を行います:
UITabBar.appearance().barTintColor = your_color