UITabBarController的简单封装

简单粗暴直接上代码

#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];
}

效果图如下:

C68CB6E8-13C5-4EA6-A9E6-A8C4A989A54B.jpg

本人第一次写简书,不足之处还望请多多指教。
如果转载请注明转于:阿龍飛

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,861评论 25 708
  • 这几天,我每天都会思考房子的相关事情,要么是房子的装修风格,要么是房子的居住效果。 我现在住的房子感觉...
    周言欣文阅读 211评论 0 0
  • 他打了我的杯子 “老师,他打了我的杯子” 赫带着哭腔跑过来跟我说。 “发生了什么事?” “~##~” 赫没有说清楚...
    请叫我郑老师阅读 368评论 0 8
  • 人与人之间平时相处的都很融洽,很投缘,一旦遇到事的时候,哪些人靠得住,哪些人靠不住就知道了。去农贸市场取货,老板对...
    張嘉宾阅读 223评论 0 1