簡單粗暴直接上代碼
#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)];
//設(shè)置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{
//設(shè)置圖片
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];
//導(dǎo)航控制器不透明
nav.navigationBar.translucent = NO;
[self addChildViewController:nav];
}
效果圖如下:

C68CB6E8-13C5-4EA6-A9E6-A8C4A989A54B.jpg
本人第一次寫簡書,不足之處還望請多多指教。
如果轉(zhuǎn)載請注明轉(zhuǎn)于:阿龍飛