objective-c UITabBar 中的自定义颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/675433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom colors in UITabBar
提问by pix0r
Is it possible to use custom colors and background images in a UITabBar? I realize that Apple would like everyone to use the same blue and gray tab bars, but is there any way to customize this?
是否可以在 UITabBar 中使用自定义颜色和背景图像?我意识到 Apple 希望每个人都使用相同的蓝色和灰色标签栏,但有没有办法自定义它?
Second, even I were to create my own TabBar-like view controller, along with custom images, would this violate Apple's Human Interface Guidelines?
其次,即使我要创建自己的类似 TabBar 的视图控制器以及自定义图像,这是否会违反Apple 的人机界面指南?
采纳答案by Filiberto Cota
I found an answer to this at Silent Mac Design.
我在Silent Mac Design找到了答案。
I implemented this way:
我是这样实现的:
First make a subclass of UITabBarContoller
首先创建一个 UITabBarContoller 的子类
// CustomUITabBarController.h
#import <UIKit/UIKit.h>
@interface CustomUITabBarController: UITabBarController {
IBOutlet UITabBar *tabBar1;
}
@property(nonatomic, retain) UITabBar *tabBar1;
@end
?
?
// CustomUITabBarController.m
#import "CustomUITabBarController.h"
@implementation CustomUITabBarController
@synthesize tabBar1;
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0.0, 0, self.view.bounds.size.width, 48);
UIView *v = [[UIView alloc] initWithFrame:frame];
[v setBackgroundColor:[[UIColor alloc] initWithRed:1.0
green:0.0
blue:0.0
alpha:0.1]];
[tabBar1 insertSubview:v atIndex:0];
[v release];
}
@end
And in your Nib file replace the class of your TabBar Controller with CustomUITabBarController.
在你的 Nib 文件中,用 CustomUITabBarController 替换你的 TabBar 控制器的类。
回答by Dan J
FYI, from iOS 5 onwards you can customize various aspects of the UITabBar, including setting its background image using the backgroundImageproperty.
仅供参考,从 iOS 5 开始,您可以自定义 UITabBar 的各个方面,包括使用backgroundImage属性设置其背景图像。
The new UITabBar"Customizing Appearance" properties in iOS 5 are:
iOS 5 中新的UITabBar“自定义外观”属性是:
backgroundImage
selectedImageTintColor
selectionIndicatorImage
tintColor
Given that Apple have introduced these methods in iOS 5, then it's possible they may be more sympathetic to attempts to customize the UITabBar for earlier OSes. This websitesays the Twitter app uses a custom tab bar, so that might be more reason that Apple would let such an app into the App Store, it's no guarantee though!
鉴于 Apple 在 iOS 5 中引入了这些方法,那么他们可能更愿意尝试为早期操作系统自定义 UITabBar。该网站称 Twitter 应用程序使用自定义标签栏,因此这可能是 Apple 允许此类应用程序进入 App Store 的更多原因,但不能保证!
回答by Sagar R. Kothari
Use Following images ( Assuming, tabBar is having 5 Tabs as follows )
使用以下图像(假设,tabBar 有 5 个标签如下)
Create a new project using - "TabBar Application" template & Place following code.
使用“TabBar Application”模板创建一个新项目并放置以下代码。
Contents of AppDel.h File.
AppDel.h 文件的内容。
#import <UIKit/UIKit.h>
@interface cTabBarAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet UIImageView *imgV;
@end
Contents of AppDel.m File.
AppDel.m 文件的内容。
#import "cTabBarAppDelegate.h"
@implementation cTabBarAppDelegate
@synthesize window=_window;
@synthesize tabBarController=_tabBarController;
@synthesize imgV = _imgV;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.tabBarController.delegate=self;
self.imgV.frame=CGRectMake(0, 425, 320, 55);
[self.tabBarController.view addSubview:self.imgV];
self.tabBarController.selectedIndex=0;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
switch (index) {
case 0:
self.imgV.image=[UIImage imageNamed:@"tBar1.png"];
break;
case 1:
self.imgV.image=[UIImage imageNamed:@"tBar2.png"];
break;
case 2:
self.imgV.image=[UIImage imageNamed:@"tBar3.png"];
break;
case 3:
self.imgV.image=[UIImage imageNamed:@"tBar4.png"];
break;
case 4:
self.imgV.image=[UIImage imageNamed:@"tBar5.png"];
break;
default:
break;
}
return YES;
}
回答by Richard Chen
At the beginning of ***ViewController.m add the following might help set background image of UITabBar.
在 ***ViewController.m 的开头添加以下内容可能有助于设置 UITabBar 的背景图像。
@implementation UITabBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed: @"background.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
@end
回答by Jens Willy Johannsen
If you want to use custom colors for the icons (and not just the background) instead of the default gray and blue, do it like this: http://blog.theanalogguy.be/2010/10/06/custom-colored-uitabbar-icons/
如果您想为图标(而不仅仅是背景)使用自定义颜色而不是默认的灰色和蓝色,请这样做:http: //blog.theanalogguy.be/2010/10/06/custom-colored- uitabbar-icons/
Basically, you need to create complete tabbar images (background and icons and text) for each selected tab and set your UITabBarItems to no icon and no title and insert the image into the tabbar as an UIImageView in viewWillAppear:
基本上,您需要为每个选定的选项卡创建完整的标签栏图像(背景和图标和文本),并将您的 UITabBarItems 设置为无图标和无标题,并将图像作为 UIImageView 插入到标签栏中的 viewWillAppear 中:
And Apple won't mind since we are not using any private APIs.
Apple 不会介意,因为我们没有使用任何私有 API。
回答by emp
Since iOS 7.0, you can use -[UIImage imageWithRenderingMode:]with UIImageRenderingModeAlwaysOriginalto preserve colors:
从 iOS 7.0 开始,您可以使用-[UIImage imageWithRenderingMode:]和UIImageRenderingModeAlwaysOriginal来保留颜色:
// Preserve the colors of the tabs.
UITabBarController *controller = (UITabBarController *)((UIWindow *)[UIApplication sharedApplication].windows[0]).rootViewController;
NSArray *onIcons = @[ @"tab1-on", @"tab2-on", @"tab3-on" ];
NSArray *offIcons = @[ @"tab1-off", @"tab2-off", @"tab3-off" ];
NSArray *items = controller.tabBar.items;
for (NSUInteger i = 0; i < items.count; ++i) {
UITabBarItem *item = items[i];
item.image = [[UIImage imageNamed:offIcons[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item.selectedImage = [[UIImage imageNamed:onIcons[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
Works like a charm.
奇迹般有效。
回答by Vineesh TP
In AppDelegate.m
在 AppDelegate.m 中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]];
return YES;
}
回答by NiKKi
Its possible without adding any subView.
它可能不添加任何子视图。
In the class where you define the tab bar set the property of the tabBarItem to ->>
在定义标签栏的类中,将 tabBarItem 的属性设置为 ->>
UITabBarItem *tabBarItem1 = [[self.tabBar.tabBar items] objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"campaigns_hover.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"campaigns.png"]];
Its a property of tabBarItem and u can change the default blue image to a custom image. campaigns_hover.png is the selected custom image AND campaigns.png is the custom image when not selected...
它是 tabBarItem 的一个属性,您可以将默认蓝色图像更改为自定义图像。campaign_hover.png 是选定的自定义图片,而 campaign.png 是未选中时的自定义图片...
Enjoy the secret.. :)
享受秘密.. :)
回答by binbash
Here's the document that says we can't change pressed or selected appearance with our icons.
这里的文档说我们不能用我们的图标改变按下或选择的外观。
It's under the heading Icons for Navigation Bars, Toolbars, and Tab Bars
它位于导航栏、工具栏和标签栏图标的标题下
回答by Jasarien
As far as the UITabBar class is concerned, the icons in the bar are limited to the colours: blue for selected and grey for unselected. This is because the tab bar only uses the alpha value from the icons you supply to create the image on the bar.
就 UITabBar 类而言,栏中的图标仅限于颜色:蓝色表示选中,灰色表示未选中。这是因为标签栏仅使用您提供的图标中的 alpha 值在栏上创建图像。
The bar itself is limited to being black, as far as I can remember. I've not seen anything like the 'tint' property on UINavigationBar in the docs.
据我所知,酒吧本身仅限于黑色。我在文档中没有看到 UINavigationBar 上的“tint”属性之类的东西。
I guess you could go ahead and create your own tab bar style class and do what you want with it, but I have absolutely no idea how that fits in with Apple's HIG, or whether or not they'd challenge it during the review process.
我想你可以继续创建自己的标签栏样式类并用它做你想做的事,但我完全不知道这如何与 Apple 的 HIG 相适应,或者他们是否会在过程中挑战它。
In my experience, Apple reviewers only rejected my app if I didn't use THEIR UI elements according to the HIG. They might have a different view when it's your own UI elements you're playing with.
根据我的经验,如果我没有根据 HIG 使用他们的 UI 元素,Apple 审核人员只会拒绝我的应用程序。当您使用自己的 UI 元素时,他们可能会有不同的看法。


