在storyBoard
上直接设置的UITabBarController
的UITabBarItem
的image
和title
,系统默认是灰色跟蓝色的,但是我们的图片是带边框的,直接在storyBoard
上设置图片只能通过设置setTintColor
来改变图片的颜色,而边框却没有了,这跟预期效果不一样。可以直接通过代码来设置:
特别说明一下:
1.UIImageRenderingModeAutomatic // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。 2.UIImageRenderingModeAlwaysOriginal // 始终绘制图片原始状态,不使用Tint Color。 3.UIImageRenderingModeAlwaysTemplate // 始终根据Tint Color绘制图片,忽略图片的颜色信息。
第一种方法:通过UITabBarItem
来设置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (!self.window.rootViewController) {
self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"UITabBarController"];
}
self.window.rootViewController.view.backgroundColor = [UIColor whiteColor];
UITabBarController *tabbar = (id)self.window.rootViewController;
UITabBarItem *item1 = tabbar.tabBar.items[0];
item1.image = [[UIImage imageNamed:@"ic_main_mine"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item1.selectedImage = [[UIImage imageNamed:@"ic_main_mine_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item1.title = @"我的";
UITabBarItem *item2 = tabbar.tabBar.items[1];
item2.image = [[UIImage imageNamed:@"ic_main_home"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item2.selectedImage = [[UIImage imageNamed:@"ic_main_home_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item2.title = @"首页";
[[UITabBar appearance] setTintColor:HSQColorTitleCoffee];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:HSQColorTitleCoffee, NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:HSQColorTitleCoffee, NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
return YES;
}
第二种方法通过控制器firstVC.tabBarItem
来设置:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (!self.window.rootViewController) {
self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"UITabBarController"];
}
self.window.rootViewController.view.backgroundColor = [UIColor whiteColor];
UITabBarController *tabbar = (id)self.window.rootViewController;
FirstViewController *firstVC = tabbar.viewControllers[0];
SecondViewController *secondVC = tabbar.viewControllers[1];
//这种方法UIImageRenderingModeAlwaysOriginal这个只有selectedImage生效,不建议使用,可以自己试试
firstVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"ic_main_home"] selectedImage:[[UIImage imageNamed:@"ic_main_home_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
//建议使用
secondVC.tabBarItem.image = [[UIImage imageNamed:@"ic_main_mine"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
secondVC.tabBarItem.selectedImage = [[UIImage imageNamed:@"ic_main_mine_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
secondVC.title = @"我的";
return YES;
}
网友评论