简单粗暴直接上代码
#import "TabBarController.h"
#import "MeViewController.h"
#import "FriendsController.h"
#import "ChatListViewController.h"
#import "NavigationController.h"
@interface TabBarController ()
@end
@implementation TabBarController
- (void)viewDidLoad {
[super viewDidLoad];
//改变UITabBarController的颜色
UIView * mView=[[UIView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,49)];
//设置UITabBarController的背景
[mView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbar.png"]]];
[mView setBackgroundColor:[UIColor yellowColor]];
mView.alpha=0.8;
[self.tabBar insertSubview:mView atIndex:1];
ChatListViewController * homeVc = [[ChatListViewController alloc]init];
[self addChildWithController:homeVc
setTabBarItem:homeVc.tabBarItem
Title:@"会话"
withTitleSize:14.0
andFoneName:@"Helvetica"
selectedImage:@"tabbar_discover_selected"
withTitleColor:[UIColor redColor]
unselectedImage:@"tabbar_discover_selected"
withTitleColor:[UIColor grayColor]];
FriendsController * friendsVc = [[FriendsController alloc]init];
[self addChildWithController:friendsVc
setTabBarItem:friendsVc.tabBarItem
Title:@"好友"
withTitleSize:14.0
andFoneName:@"Helvetica"
selectedImage:@"tabbar_discover_selected"
withTitleColor:[UIColor redColor]
unselectedImage:@"tabbar_discover_selected"
withTitleColor:[UIColor grayColor]];
MeViewController * mineVc = [[MeViewController alloc]init];
[self addChildWithController:mineVc
setTabBarItem:mineVc.tabBarItem
Title:@"我的"
withTitleSize:14.0
andFoneName:@"Helvetica"
selectedImage:@"tabbar_discover_selected"
withTitleColor:[UIColor redColor]
unselectedImage:@"tabbar_discover_selected"
withTitleColor:[UIColor grayColor]];
}
#pragma mark - 封装添加子控制器的方法
- (void)addChildWithController:(UIViewController*)childVc
setTabBarItem:(UITabBarItem *)tabbarItem
Title:(NSString *)title
withTitleSize:(CGFloat)size
andFoneName:(NSString *)foneName
selectedImage:(NSString *)selectedImage
withTitleColor:(UIColor *)selectColor
unselectedImage:(NSString *)unselectedImage
withTitleColor:(UIColor *)unselectColor{
//设置图片
tabbarItem = [tabbarItem initWithTitle:title image:[[UIImage imageNamed:unselectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
//未选中字体颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:unselectColor,NSFontAttributeName:[UIFont fontWithName:foneName size:size]} forState:UIControlStateNormal];
//选中字体颜色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:selectColor,NSFontAttributeName:[UIFont fontWithName:foneName size:size]} forState:UIControlStateSelected];
NavigationController *nav = [[NavigationController alloc]initWithRootViewController:childVc];
//导航控制器不透明
nav.navigationBar.translucent = NO;
[self addChildViewController:nav];
}
效果图如下:
本人第一次写简书,不足之处还望请多多指教。
如果转载请注明转于:阿龍飛