iOS 關(guān)于tabBar

1. 創(chuàng)建一個帶tabBar的App

一般項目中的App界面框架結(jié)構(gòu),如下:

App界面框架結(jié)構(gòu)

<figcaption></figcaption>

本例中創(chuàng)建了一個QiTabBarController繼承于UITabBarController年叮,并作為window的rootViewController,則在QiTabBarController中寫以下代碼即可實現(xiàn)上面所述結(jié)構(gòu)蒲凶。

//// AppDelegate.m 中代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    QiTabBarController *tabBarController = [[QiTabBarController alloc] init];
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [_window setBackgroundColor:[UIColor whiteColor]];
    [_window setRootViewController:tabBarController];
    [_window makeKeyAndVisible];

    return YES;
}

//// QiTabBarController.m 中代碼
#import "QiTabBarController.h"
#import "QiNavigationController.h"
#import "FirstController.h"
#import "SecondController.h"

@interface QiTabBarController ()

@end

@implementation QiTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupChildControllers];
}

- (void)setupChildControllers {

    FirstController *first = [[FirstController alloc] init];
    QiNavigationController *firstNav = [[QiNavigationController alloc] initWithRootViewController:first];
    firstNav.tabBarItem.title = @"FirTab";
    firstNav.tabBarItem.image = [[UIImage imageNamed:@"tab_team_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    firstNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"tab_team_50"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    SecondController *second = [[SecondController alloc] init];
    QiNavigationController *secondNav = [[QiNavigationController alloc] initWithRootViewController:second];
    secondNav.tabBarItem.title = @"SecTab";
    secondNav.tabBarItem.image = [[UIImage imageNamed:@"tab_mine_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    secondNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"tab_mine_50"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    self.viewControllers = @[firstNav, secondNav, thirdNav, fourthNav, fifthNav, sixthNav];
}

@end
復(fù)制代碼

如果App設(shè)計有底部標(biāo)簽欄(UITabBar)垄惧,我們可以通過Xcode的調(diào)試功能“Debug View Hierarchy”看到UITabBar的結(jié)構(gòu)。

tabBar中子view的層次結(jié)構(gòu)

<figcaption></figcaption>

當(dāng)tabBar中的UITabItem個數(shù)超過5個時嘹害,tabBar右側(cè)會出現(xiàn)一個more按鈕,點擊more按鈕進(jìn)入一個名為more的controller吮便,點擊展示出來的其他tabItem即可可進(jìn)入相應(yīng)的controller笔呀。

tabBar中的“更多”

<figcaption></figcaption>

其中,名為more的controller導(dǎo)航欄右側(cè)有edit按鈕髓需,點擊進(jìn)入后许师,可拖動編輯tabBar中所示的tab順序。在拖動編輯tabBar中所示的tab順序時僚匆,系統(tǒng)會自動調(diào)用UITabBarDelegate中的相應(yīng)方法(如果需要微渠,可在QiTabBarController中直接實現(xiàn)UITabBarDelegate協(xié)議方法):

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item; // called when a new view is selected by the user (but not programatically)

/* called when user shows or dismisses customize sheet. you can use the 'willEnd' to set up what appears underneath. 
 changed is YES if there was some change to which items are visible or which order they appear. If selectedItem is no longer visible, 
 it will be set to nil.
 */

- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray<UITabBarItem *> *)items __TVOS_PROHIBITED;                     // called before customize sheet is shown. items is current item list
- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray<UITabBarItem *> *)items __TVOS_PROHIBITED;                      // called after customize sheet is shown. items is current item list
- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed __TVOS_PROHIBITED; // called before customize sheet is hidden. items is new item list
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed __TVOS_PROHIBITED;  // called after customize sheet is hidden. items is new item list
復(fù)制代碼

2. 設(shè)置tabBar的樣式

  • 設(shè)置tabBar的基本樣式 在普通的項目中,一般只需要修改tabBar中每個item展示的圖片icon和title及item的位置咧擂,或item中icon與title的間隔逞盆。
- (void)setTabBarStyle {

    //去掉TabBar頂部的線
    UITabBar *tabBar = self.tabBar;
    [tabBar setShadowImage:[UIImage new]];
    [tabBar setBackgroundImage:[UIImage new]];
    tabBar.translucent = NO;

    for (UITabBarItem *item in self.tabBar.items) {
        // 設(shè)置UITabBarItem中title樣式
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]} forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateSelected];

        // 設(shè)置UITabBarItem中按鈕大小,及img與title間距
        [item setImageInsets:UIEdgeInsetsMake(-5, 0, 5, 0)];
    }
}
復(fù)制代碼
  • 自定義tabBar按鈕動畫 一般常見的tabBar按鈕動畫有兩種:點擊時icon縮小放大動畫松申,icon像.gif型圖片一樣的動畫云芦。
    tabBar按鈕動畫

    <figcaption></figcaption>

需求看起來很簡單俯逾,我們應(yīng)該已經(jīng)有了思路,首先舅逸,我們應(yīng)該找到這個當(dāng)前我們要操作的這個tabBarBtn桌肴, 在QiTabBarController中實現(xiàn)UITabBarControllerDelegate方法,其中可以監(jiān)聽到tabBar上的點擊動作:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    NSInteger index = [tabBarController.childViewControllers indexOfObject:viewController];

    UIButton *tabBarBtn = tabBarController.tabBar.subviews[index+1];
    UIImageView *imageView = tabBarBtn.subviews.firstObject;

    // 我們把動畫加到這個imageView上琉历,即可實現(xiàn)動畫效果

    // ......

    return YES;
}
復(fù)制代碼

在本文實例中坠七,我們只設(shè)置了兩個tab項,第一個tabBarBtn使用.gif型動畫善已,第二個tabBarBtn使用縮小放大動畫灼捂,整個QiTabBarController實現(xiàn)代碼如下:

#import "QiTabBarController.h"
#import "QiNavigationController.h"
#import "FirstController.h"
#import "SecondController.h"

@interface QiTabBarController () <UITabBarControllerDelegate>

@property (strong, nonatomic) NSMutableArray<UIImage *> *imgArr;

@end

@implementation QiTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setupChildControllers];
    [self setTabBarStyle];
    [self initImages];
}

- (void)setupChildControllers {

    self.view.backgroundColor = [UIColor whiteColor];
    self.delegate = self;

    FirstController *first = [[FirstController alloc] init];
    QiNavigationController *firstNav = [[QiNavigationController alloc] initWithRootViewController:first];
    firstNav.tabBarItem.title = @"FirTab";
    firstNav.tabBarItem.image = [[UIImage imageNamed:@"tab_team_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    firstNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"tab_team_50"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    SecondController *second = [[SecondController alloc] init];
    QiNavigationController *secondNav = [[QiNavigationController alloc] initWithRootViewController:second];
    secondNav.tabBarItem.title = @"SecTab";
    secondNav.tabBarItem.image = [[UIImage imageNamed:@"tab_mine_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    secondNav.tabBarItem.selectedImage = [[UIImage imageNamed:@"tab_mine_50"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    self.viewControllers = @[firstNav, secondNav];
}

- (void)setTabBarStyle {

    //去掉TabBar頂部的線
    UITabBar *tabBar = self.tabBar;
    [tabBar setShadowImage:[UIImage new]];
    [tabBar setBackgroundImage:[UIImage new]];
    tabBar.translucent = NO;

    for (UITabBarItem *item in self.tabBar.items) {
        // 設(shè)置UITabBarItem中title樣式
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor lightGrayColor]} forState:UIControlStateNormal];
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateSelected];

        // 設(shè)置UITabBarItem中按鈕大小,及img與title間距
        [item setImageInsets:UIEdgeInsetsMake(-3, 0, 3, 0)];
    }
}

- (void)initImages {

    _imgArr = [NSMutableArray array];
    for (int i=0; i<51; i++) {
        NSString *name = [NSString stringWithFormat:@"tab_team_%02d", i];
        UIImage *image = [UIImage imageNamed:name];
        [_imgArr addObject:image];
    }
}

- (CAAnimation *)getCustomAnimation {

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    //速度控制函數(shù)换团,控制動畫運行的節(jié)奏
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.duration = 0.2;
    animation.repeatCount = 1;
    animation.autoreverses = YES;
    animation.fromValue = [NSNumber numberWithFloat:0.7];
    animation.toValue = [NSNumber numberWithFloat:1.2];

    return animation;
}

#pragma mark - UITabBarControllerDelegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    NSInteger index = [tabBarController.childViewControllers indexOfObject:viewController];

    UIButton *tabBarBtn = tabBarController.tabBar.subviews[index+1];
    UIImageView *imageView = tabBarBtn.subviews.firstObject;

    if (index == 0) {
        [imageView stopAnimating];
        imageView.animationImages = _imgArr;
        imageView.animationRepeatCount = 1;
        imageView.animationDuration = 0.7;
        [imageView startAnimating];
    } else {
        static NSString *tabBarBtnAnimationKey = @"tabBarBtnAnimationKey";
        [imageView.layer removeAnimationForKey:tabBarBtnAnimationKey];
        [imageView.layer addAnimation:[self getCustomAnimation] forKey:tabBarBtnAnimationKey];
    }

    return YES;
}

@end
復(fù)制代碼

我們可在getCustomAnimation中定義其他類型的動畫悉稠,來滿足不同需求。
工程源碼GitHub地址

作者:QiShare
鏈接:https://juejin.im/post/5cd0043de51d453ae11053ff
來源:掘金
著作權(quán)歸作者所有艘包。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán)的猛,非商業(yè)轉(zhuǎn)載請注明出處。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末想虎,一起剝皮案震驚了整個濱河市卦尊,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌舌厨,老刑警劉巖岂却,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異裙椭,居然都是意外死亡躏哩,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進(jìn)店門揉燃,熙熙樓的掌柜王于貴愁眉苦臉地迎上來扫尺,“玉大人,你說我怎么就攤上這事炊汤≌ぃ” “怎么了?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵抢腐,是天一觀的道長姑曙。 經(jīng)常有香客問我,道長迈倍,這世上最難降的妖魔是什么渣磷? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮授瘦,結(jié)果婚禮上醋界,老公的妹妹穿的比我還像新娘。我一直安慰自己提完,他們只是感情好形纺,可當(dāng)我...
    茶點故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著徒欣,像睡著了一般逐样。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上打肝,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天脂新,我揣著相機與錄音,去河邊找鬼粗梭。 笑死争便,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的断医。 我是一名探鬼主播滞乙,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼鉴嗤!你這毒婦竟也來了斩启?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤醉锅,失蹤者是張志新(化名)和其女友劉穎兔簇,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體硬耍,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡垄琐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了默垄。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片此虑。...
    茶點故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖口锭,靈堂內(nèi)的尸體忽然破棺而出朦前,到底是詐尸還是另有隱情,我是刑警寧澤鹃操,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布韭寸,位于F島的核電站,受9級特大地震影響荆隘,放射性物質(zhì)發(fā)生泄漏恩伺。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一椰拒、第九天 我趴在偏房一處隱蔽的房頂上張望晶渠。 院中可真熱鬧凰荚,春花似錦、人聲如沸褒脯。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽番川。三九已至到涂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間颁督,已是汗流浹背践啄。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留沉御,地道東北人屿讽。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓,卻偏偏與公主長得像嚷节,于是被迫代替她去往敵國和親聂儒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,901評論 2 345

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