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)];
    //設(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)于:阿龍飛

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,001評論 25 709
  • 這幾天,我每天都會思考房子的相關(guān)事情,要么是房子的裝修風(fēng)格,要么是房子的居住效果。 我現(xiàn)在住的房子感覺...
    周言欣文閱讀 243評論 0 0
  • 他打了我的杯子 “老師,他打了我的杯子” 赫帶著哭腔跑過來跟我說。 “發(fā)生了什么事?” “~##~” 赫沒有說清楚...
    請叫我鄭老師閱讀 458評論 0 8
  • 人與人之間平時相處的都很融洽,很投緣,一旦遇到事的時候,哪些人靠得住,哪些人靠不住就知道了。去農(nóng)貿(mào)市場取貨,老板對...
    張嘉賓閱讀 271評論 0 1

友情鏈接更多精彩內(nèi)容