设置tabBar的图片/高度/title颜色
实现了一下内容:
1.设置tabBarItem选中及非选中时的图片,图片充满item;
2.调整了 tabBar 高度;
3.改变了title颜色及位置.
------------代码如下:
---TabBarC.m---
#import "TabBarC.h"
#import "ViewController.h"
#import "SecViewController.h"
#define CustomTabBarHeight 60//不需要改变高度就换成self.tabBar.frame.size.height
#define CustomTabBarWidth self.tabBar.frame.size.width
@interface TabBarC ()
@end
@implementation TabBarC
- (void)viewDidLoad {
[super viewDidLoad];
//初始化子控制器
ViewController *onevc = [[ViewController alloc] init];
SecViewController *twovc = [[SecViewController alloc] init];
//将VC, 未选中图,选中图 放到数组里
NSArray *onevcArr = @[onevc,@"one_icon",@"sel_one_icon"];
NSArray *twovcArr = @[twovc,@"two_icon",@"sel_two_icon"];
NSArray *vcArr = @[onevcArr,twovcArr];
[self addToTabBar:vcArr];
}
//添加子控制器到 tabBar
- (void)addToTabBar:(NSArray *)array{
for (NSInteger i=0; i<array.count; i++) {
NSArray *everyVCArr = array[i];
UIViewController *everyVC = everyVCArr[0];
everyVC.title = @"vc标题";
everyVC.view.backgroundColor = [UIColor whiteColor];
//将图片插入的位置向下移动5.5 ; top和 bottom不为相反数点击时图片会变
everyVC.tabBarItem.imageInsets = UIEdgeInsetsMake(5.5, 0, -5.5, 0);
//tabBarItem的宽
NSInteger itemWidth = CustomTabBarWidth/array.count;
#pragma -------注意:(不设置tabBarItem.image的话,其他tabBarItem的设置都无效)
#pragma -------这里没有准备不同尺寸的合适的图片,为了展示效果直接将图片缩放到合适尺寸,缩放步骤可省略
//--------设置非选中时的图片,图设置为原始状态
everyVC.tabBarItem.image = [[self editImage:everyVCArr[1] toSize:CGSizeMake(itemWidth, CustomTabBarHeight)]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//设置选中时的图片,图设置为原始状态
everyVC.tabBarItem.selectedImage = [[self editImage:everyVCArr[2] toSize:CGSizeMake(itemWidth, CustomTabBarHeight)]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//调整 tabBarItem 标题位置
[everyVC.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, 5)];
//设置字体颜色
NSMutableDictionary *textAttribute = [NSMutableDictionary dictionary];
textAttribute[NSForegroundColorAttributeName] = [UIColor blackColor];
[everyVC.tabBarItem setTitleTextAttributes:textAttribute forState:UIControlStateNormal];
//设置字体颜色
[everyVC.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateSelected];
everyVC.tabBarItem.title = @"啦啦啦啦啦啦";
UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:everyVC];
[self addChildViewController:nvc];
}
NSLog(@"🍎tabBar高度🍎%f",self.tabBar.frame.size.height);
}
//改变 tabBar 默认高度
- (void)viewWillLayoutSubviews{
CGRect tabBarFrame = self.tabBar.frame;
tabBarFrame.size.height = CustomTabBarHeight;
tabBarFrame.origin.y = self.view.frame.size.height - CustomTabBarHeight;
self.tabBar.frame = tabBarFrame;
}
//将图片缩放成指定尺寸
- (UIImage *)editImage:(NSString *)imageName toSize:(CGSize)reSize{
UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
[[UIImage imageNamed:imageName]drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return reSizeImage;
}
//不隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
return NO;
}
//状态栏样式设置为白色
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
---AppDelegate.m---
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
TabBarC *tabBarC = [[TabBarC alloc]init];
self.window.rootViewController = tabBarC;
[self.window makeKeyAndVisible];
return YES;
}
END