Day.03.08 UITabBarController 自定義標(biāo)簽控制器

AppDelegate.m


#import "AppDelegate.h"

#import "CustomTabBarController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    [self.window makeKeyAndVisible];
    
    /*——————————————————————————————————————————————————————————————————————————————-*/
    
    UIViewController *vc0 = [[UIViewController alloc]init];
    UIViewController *vc1 = [[UIViewController alloc]init];
    UIViewController *vc2 = [[UIViewController alloc]init];
    UIViewController *vc3 = [[UIViewController alloc]init];
    UIViewController *vc4 = [[UIViewController alloc]init];

    vc0.title = @"HOME";
    vc0.tabBarItem.image = [UIImage imageNamed:@"home.png"];
    vc1.title = @"INFO";
    vc1.tabBarItem.image = [UIImage imageNamed:@"myinfo.png"];
    vc2.title = @"payticket";
    vc2.tabBarItem.image = [UIImage imageNamed:@"payticket"];
    vc3.title = @"discover";
    vc3.tabBarItem.image = [UIImage imageNamed:@"discover"];
    vc4.title = @"store";
    vc4.tabBarItem.image = [UIImage imageNamed:@"store"];
    
    UINavigationController *nav0 = [[UINavigationController alloc]initWithRootViewController:vc0];
    UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:vc1];
    UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:vc2];
    UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:vc3];
    UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:vc4];
    
    
    //1.
    CustomTabBarController *tbc = [[CustomTabBarController alloc]initWithSelectedImge:[UIImage imageNamed:@"選中"] tabBarBackgroundImage:[UIImage imageNamed:@"tab_bg_all"]];
    
    //2.
    tbc.viewControllers = @[nav0,nav1,nav2,nav3,nav4];
    
    tbc.selectedIndex = 2;
    
    /**
     *  分析空間功能和信息來(lái)源
     
        1.當(dāng)設(shè)置viewControllers的時(shí)候 -> 按鈕個(gè)數(shù) 按鈕寬度
     
        2.item的image和title 來(lái)源是 -> VC控制器
     
        3.選中圖片的位置 -> 點(diǎn)擊item調(diào)用的代理方法 index
     
        4.背景圖 -> 子類(lèi)化得標(biāo)簽控制器的屬性
     */
    
    self.window.rootViewController = tbc;

    
    return YES;
}


@end

CustomTabBarController.h

#import <UIKit/UIKit.h>

@class CustomTabBarItem;

@interface CustomTabBarController : UITabBarController

- (instancetype)initWithSelectedImge:(UIImage *)selectedImage tabBarBackgroundImage:(UIImage *)bgImage;

@end

@interface CustomTabBarItem : UIButton

/**
 *  通過(guò)frame和tabBarItem創(chuàng)建按鈕的方法
 */

- (instancetype)initWithFrame:(CGRect)frame tabBarItem:(UITabBarItem *)tabBarItem;

@end

CustomTabBarController.m

#import "CustomTabBarController.h"

#define KScreenWidth [UIScreen mainScreen].bounds.size.width

#define KScreenHeight [UISreen mainScreen].bounds.size.height

@interface CustomTabBarController ()

@property (nonatomic,strong) UIView *customTabBar;

@property (nonatomic,strong) UIImage *selectImg;

@property (nonatomic,strong) UIImage *bgImg;//私有屬性保存背景圖片

@property (nonatomic,strong) UIImageView *selectIV;//選中item背景圖片視圖

@end

@implementation CustomTabBarController


- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    
    _selectIV.frame = CGRectMake(10 +self.selectedIndex *KScreenWidth/self.viewControllers.count, 5, KScreenWidth/self.viewControllers.count -20, 44);
}

- (instancetype)initWithSelectedImge:(UIImage *)selectedImage tabBarBackgroundImage:(UIImage *)bgImage{

    if (self = [super init]) {
        
        _selectImg = selectedImage;
        
        _bgImg = bgImage;
    }
    return self;
}

//重
- (void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers{

    [super setViewControllers:viewControllers];
    /**
     *  
     執(zhí)行完上面的方法
     
     可以得到當(dāng)前本控制器管理的視圖控制器個(gè)數(shù)
     
     1.移除原有的按鈕 tabbarItem
     2.添加自定義的 tabbarItem
     3.更改背景圖片
     4.自定義的選中圖片
     */
    
    [self removeTabBarButtons];
    
    [self createNewTaBar];
    
    [self createTabBarButton];
    
}

#pragma mark --1.移除原有的所有tabBarButton
/**
 *  1.移除tabbar上所有的tabbarItem
 */
- (void)removeTabBarButtons{

    NSLog(@"%@",self.tabBar.subviews);
    
    for (UIView *subView in self.tabBar.subviews) {
        
        /**
         *  NSClassFromString(NSString *aClassName) -> 將字符串轉(zhuǎn)化為對(duì)應(yīng)的類(lèi)名
         
         UITabBarButton -> 標(biāo)簽控制器定義的內(nèi)部類(lèi)[tabbar上的按鈕]
         */

        if ([subView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            
            [subView removeFromSuperview];
        }
    }
}

/**
 *  2.創(chuàng)建新的有背景圖的tabbar
 */
- (void)createNewTaBar{

    _customTabBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 49)];
    
    _customTabBar.backgroundColor = [UIColor colorWithPatternImage:[_bgImg stretchableImageWithLeftCapWidth:160 topCapHeight:25]];
    
    [self.tabBar addSubview:_customTabBar];
}

/**
 *  3.創(chuàng)建標(biāo)簽按鈕
 */
- (void)createTabBarButton{

    //1.獲取控制器個(gè)數(shù)
    NSInteger count = self.viewControllers.count;
    
    //2.選中的背景圖片
    _selectIV = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, KScreenWidth/count -20, 44)];
    
    _selectIV.image = _selectImg;
    
    [_customTabBar addSubview:_selectIV];
    
    /*——————————————————————————————————————————————————————————————————————————————-*/
    
    //創(chuàng)建按鈕
    //1.根據(jù)個(gè)數(shù)創(chuàng)建按鈕
    for (int i = 0; i <count; i++) {
        
        //計(jì)算按鈕的frame
        CGRect itemFrame = CGRectMake(i *KScreenWidth/count, 0, KScreenWidth/count, 49);
        
        //獲取按鈕對(duì)應(yīng)的控制器的item
        UITabBarItem *tabBarItem = self.viewControllers[i].tabBarItem;
        
        //創(chuàng)建
        CustomTabBarItem *item = [[CustomTabBarItem alloc]initWithFrame:itemFrame tabBarItem:tabBarItem];
        
        //tag值
        item.tag = 100 +i;
        
        //添加事件
        [item addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [_customTabBar addSubview:item];
    }
    
}

- (void)itemClick:(UIButton *)item{

//切換視圖控制器
    //1.獲取點(diǎn)擊按鈕的index
    NSInteger index = item.tag - 100;
    
    //2.更改當(dāng)前顯示的VC
    self.selectedIndex = index;
    
    /*——————————————————————————————————————————————————————————————————————————————-*/
    
    //選中圖片移動(dòng)動(dòng)畫(huà)
    
    [UIView animateWithDuration:0.25 animations:^{
        
        _selectIV.frame = CGRectMake(10 +self.selectedIndex *KScreenWidth/self.viewControllers.count, 5, KScreenWidth/self.viewControllers.count -20, 44);
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];

}


@end



@implementation CustomTabBarItem

- (instancetype)initWithFrame:(CGRect)frame tabBarItem:(UITabBarItem *)tabBarItem{

    if (self = [super initWithFrame:frame]) {
        
        /**
         *  tabBarItem:包含創(chuàng)建item所需要的title和image
         */
        //創(chuàng)建ImageView Label
        
        //1.Label
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, 20)];
        
        //居中
        label.textAlignment = NSTextAlignmentCenter;
        
        //字號(hào)
        label.font = [UIFont systemFontOfSize:10];
        
        //顏色
        label.textColor = [UIColor whiteColor];
        
        //內(nèi)容 --> 從tabBarItem獲取
        label.text = tabBarItem.title;
        
        [self addSubview:label];
        
        //2.Image
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((frame.size.width-29)/2, 20, 29, 29)];
        
        //內(nèi)容模式
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        
        //圖片 --> 從tabbarItem獲取
        imageView.image = tabBarItem.image;
        
        [self addSubview:imageView];
    }

    return self;
}

@end

屏幕快照 2016-03-08 下午8.47.47.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末赴精,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子凸舵,更是在濱河造成了極大的恐慌祖娘,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件啊奄,死亡現(xiàn)場(chǎng)離奇詭異渐苏,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)菇夸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)琼富,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人庄新,你說(shuō)我怎么就攤上這事鞠眉。” “怎么了择诈?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵械蹋,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我羞芍,道長(zhǎng)哗戈,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任荷科,我火速辦了婚禮唯咬,結(jié)果婚禮上纱注,老公的妹妹穿的比我還像新娘。我一直安慰自己胆胰,他們只是感情好狞贱,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著蜀涨,像睡著了一般瞎嬉。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上勉盅,一...
    開(kāi)封第一講書(shū)人閱讀 51,365評(píng)論 1 302
  • 那天佑颇,我揣著相機(jī)與錄音,去河邊找鬼草娜。 笑死,一個(gè)胖子當(dāng)著我的面吹牛痒筒,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼钉汗,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼爷绘!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起老充,我...
    開(kāi)封第一講書(shū)人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤葡盗,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后啡浊,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體觅够,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年巷嚣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了喘先。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡廷粒,死狀恐怖窘拯,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情坝茎,我是刑警寧澤涤姊,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站嗤放,受9級(jí)特大地震影響思喊,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜斤吐,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一搔涝、第九天 我趴在偏房一處隱蔽的房頂上張望厨喂。 院中可真熱鬧,春花似錦庄呈、人聲如沸蜕煌。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)斜纪。三九已至,卻和暖如春文兑,著一層夾襖步出監(jiān)牢的瞬間盒刚,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工绿贞, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留因块,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓籍铁,卻偏偏與公主長(zhǎng)得像涡上,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子拒名,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容