UITabbarController,UITabbar

一:首先查看一下關于UITabBarController的定義

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarController : UIViewController <UITabBarDelegate, NSCoding>
//設置控制器數(shù)組
@property(nullable, nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
//設置控制器數(shù)組 動畫
- (void)setViewControllers:(NSArray<__kindof UIViewController *> * __nullable)viewControllers animated:(BOOL)animated;
//選中的控制器
@property(nullable, nonatomic, assign) __kindof UIViewController *selectedViewController; 
//選中索引值
@property(nonatomic) NSUInteger selectedIndex;
//當item超過五個時 就會有一個更多
@property(nonatomic, readonly) UINavigationController *moreNavigationController; 

@property(nullable, nonatomic, copy) NSArray<__kindof UIViewController *> *customizableViewControllers; 
//tab條
@property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0); 
//委托
@property(nullable, nonatomic,weak) id<UITabBarControllerDelegate> delegate;

@end

UITabBarController和UINavigationController一樣是用來管理試圖控制器的,與導航控制器不同,tabBarController控制器使用數(shù)組管理子試圖控制器的,并且子試圖之間是平等關系,導航控制器所管理的試圖控制器之間是在出桟和入桟的關系;

二:UITabBarControllerDelegate委托內(nèi)容

@protocol UITabBarControllerDelegate <NSObject>
@optional
//返回NO,不能顯示選中的視圖控制器
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController NS_AVAILABLE_IOS(3_0);

// NSLog(@"視圖顯示后調(diào)用");
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;

//將要開始自定義item時調(diào)用
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;

//將要結(jié)束自定義item時調(diào)用
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED;

//結(jié)束自定義item的順序
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed __TVOS_PROHIBITED;

- (UIInterfaceOrientationMask)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
- (UIInterfaceOrientation)tabBarControllerPreferredInterfaceOrientationForPresentation:(UITabBarController *)tabBarController NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;

- (nullable id <UIViewControllerInteractiveTransitioning>)tabBarController:(UITabBarController *)tabBarController
                      interactionControllerForAnimationController: (id <UIViewControllerAnimatedTransitioning>)animationController NS_AVAILABLE_IOS(7_0);

- (nullable id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
            animationControllerForTransitionFromViewController:(UIViewController *)fromVC
                                              toViewController:(UIViewController *)toVC  NS_AVAILABLE_IOS(7_0);

@end

三:UIViewController (UITabBarControllerItem)分類

@interface UIViewController (UITabBarControllerItem)
//當前視圖的UITabBarItem對象
@property(null_resettable, nonatomic, strong) UITabBarItem *tabBarItem; 
//如果視圖控制器是一個標簽欄控制器的子控制器,則返回它。否則返回nil
@property(nullable, nonatomic, readonly, strong) UITabBarController *tabBarController;

@end

四:關于UITabBar的定義

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBar : UIView
//委托
@property(nullable,nonatomic,assign) id<UITabBarDelegate> delegate;     
//UITabBarItem集合
@property(nullable,nonatomic,copy) NSArray<UITabBarItem *> *items;      
//被選中的item
@property(nullable,nonatomic,assign) UITabBarItem *selectedItem; 
//批量設置items
- (void)setItems:(nullable NSArray<UITabBarItem *> *)items animated:(BOOL)animated; 

- (void)beginCustomizingItems:(NSArray<UITabBarItem *> *)items;   

- (BOOL)endCustomizingAnimated:(BOOL)animated;    

- (BOOL)isCustomizing;

//渲染色
@property(null_resettable, nonatomic,strong) UIColor *tintColor NS_AVAILABLE_IOS(5_0);
//背景色
@property(nullable, nonatomic,strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR; 
//被選中的圖片選染色
@property(nullable,nonatomic,strong) UIColor *selectedImageTintColor NS_DEPRECATED_IOS(5_0,8_0,"Use tintColor") UI_APPEARANCE_SELECTOR;
//背景圖
@property(nullable, nonatomic,strong) UIImage *backgroundImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
//選中指示圖
@property(nullable, nonatomic,strong) UIImage *selectionIndicatorImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR; 
//陰影圖
@property(nullable, nonatomic,strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;


@property(nonatomic) UITabBarItemPositioning itemPositioning NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
//元素寬度
@property(nonatomic) CGFloat itemWidth NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
//item間隔
@property(nonatomic) CGFloat itemSpacing NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
//風格
@property(nonatomic) UIBarStyle barStyle NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;
//是否透明
@property(nonatomic,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(7_0);
@end

UITabBar包含多個UITabBarItem,每?個UITabBarItem對應?個UIViewController。UITabBar的?度是49;系統(tǒng)最多只顯?5個UITabBarItem,當UITabBarItem超過5個時系統(tǒng)會?動增加?個更多按鈕,點擊更多按鈕沒有在底部出現(xiàn)的按鈕會以列表的形式顯?出來.

五:UITabBarDelegate內(nèi)容

@protocol UITabBarDelegate<NSObject>
@optional

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

- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray<UITabBarItem *> *)items;                  

- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray<UITabBarItem *> *)items;                     

- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed; 

- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed; 

@end

對于點擊哪個UITabBarItem響應事件則是在這個委托里面進行;由于UITabBarController已經(jīng)遵守了UITabBarDelegate協(xié)議,如果有繼承UITabBarController已經(jīng)可以直接運用UITabBarDelegate里面的內(nèi)容;

知識點1:iOS 點擊UITabBar觸發(fā)刷新
平常我們在切換UITabBarController底部菜單時,它是不會有刷新的功能;如果要實現(xiàn)刷新的效果可以如下實現(xiàn);原理如下,在監(jiān)聽UITabBar點擊的方法中判斷本次點擊的UITabBarItem和上次點擊的是否一樣,如果一樣就發(fā)出通知,首先需要自定義一個UITabBarController (比如LLTabBarController);要判斷本次點擊的UITabBarItem和上次點擊的是否一樣,就需要定義一個屬性記錄下來上次點擊的UITabBarItem,并在viewWillAppear:方法中給該屬性賦值默認的UITabBarItem

/** 之前被選中的UITabBarItem */
@property (nonatomic, strong) UITabBarItem *lastItem;

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 將默認被選中的tabBarItem保存為屬性
    self.lastItem = self.tabBar.selectedItem;
}


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    // 判斷本次點擊的UITabBarItem是否和上次的一樣
    if (item == self.lastItem) { // 一樣就發(fā)出通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"LLTabBarDidClickNotification" object:nil userInfo:nil];
    }
    // 將這次點擊的UITabBarItem賦值給屬性
    self.lastItem = item;
}

在需要實現(xiàn)點擊UITabBar觸發(fā)刷新功能的控制器中監(jiān)聽通知

- (void)viewDidLoad {
    [super viewDidLoad];
    // 監(jiān)聽UITabBarItem被重復點擊時的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tabBarDidClick) name:@"LLTabBarDidClickNotification" object:nil];
}


- (void)tabBarDidClick
{
    // 如果本控制器的view顯示在最前面,就下拉刷新
    if ([self.view isShowingOnKeyWindow]) { // 判斷一個view是否顯示在根窗口上,該方法在UIView的分類中實現(xiàn)
        [self.tableView.header beginRefreshing]; // MJRefresh
    }
}

判斷一個view是否顯示在根窗口上

/** 該方法在UIView的分類中實現(xiàn) */
- (BOOL)isShowingOnKeyWindow
{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    // 把這個view在它的父控件中的frame(即默認的frame)轉(zhuǎn)換成在window的frame
    CGRect convertFrame = [self.superview convertRect:self.frame toView: keyWindow];
    CGRect windowBounds = keyWindow.bounds;
    // 判斷這個控件是否在主窗口上(即該控件和keyWindow有沒有交叉)
    BOOL isOnWindow = CGRectIntersectsRect(convertFrame, windowBounds);
    // 再判斷這個控件是否真正顯示在窗口范圍內(nèi)(是否在窗口上,是否為隱藏,是否透明)
    BOOL isShowingOnWindow = (self.window == keyWindow) && !self.isHidden && (self.alpha > 0.01) && isOnWindow;
    return isShowingOnWindow;
}

在控制器銷毀時要移除通知

- (void)dealloc{ // 移除通知
 [[NSNotificationCenter defaultCenter] removeObserver:self];}

六:UITabBarItem的定義

NS_CLASS_AVAILABLE_IOS(2_0) @interface UITabBarItem : UIBarItem 

- (instancetype)init NS_DESIGNATED_INITIALIZER;
//初始化幾中方式
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image tag:(NSInteger)tag;
//初始化 標是 圖標 選中圖標
- (instancetype)initWithTitle:(nullable NSString *)title image:(nullable UIImage *)image selectedImage:(nullable UIImage *)selectedImage NS_AVAILABLE_IOS(7_0);

- (instancetype)initWithTabBarSystemItem:(UITabBarSystemItem)systemItem tag:(NSInteger)tag;

//給當前的分欄控制器的item設置一個選中狀態(tài)的圖片
@property(nullable, nonatomic,strong) UIImage *selectedImage NS_AVAILABLE_IOS(7_0);
//角標
@property(nullable, nonatomic, copy) NSString *badgeValue;    

//IOS7以后過期
- (void)setFinishedSelectedImage:(nullable UIImage *)selectedImage withFinishedUnselectedImage:(nullable UIImage *)unselectedImage NS_DEPRECATED_IOS(5_0,7_0,"Use initWithTitle:image:selectedImage: or the image and selectedImage properties along with UIImageRenderingModeAlwaysOriginal");

//IOS7以后過期
- (nullable UIImage *)finishedSelectedImage NS_DEPRECATED_IOS(5_0,7_0);
//IOS7以后過期
- (nullable UIImage *)finishedUnselectedImage NS_DEPRECATED_IOS(5_0,7_0);
//文字的偏移
@property (nonatomic, readwrite, assign) UIOffset titlePositionAdjustment NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

@end

知識點1:tabBarItem相關屬性運用

1) 用來控制一組控制器的切換,類似選項卡,每個Tab控制一個試圖控制器,點擊哪個tab就顯示對應的試圖控制器,當前的試圖控制器

  2) 每個tabBarItem都可以設置title、image/selectedImages、badgeValue
  例如:

    (1).給當前的分欄控制器的item設置一個標題
          self.tabBarItem.title = @"我的";
    (2).給當前的分欄控制器的item設置一個圖片

          self.tabBarItem.image = [UIImage imageNamed:@"tab_buddy_nor@2x"];
    (3).給當前的分欄控制器的item設置一個選中狀態(tài)的圖片

          self.tabBarItem.selectedImage = [UIImage imageNamed:@"tab_me_nor@2x"];//@2x表示給高清屏 30*30的效果好

          self.tabBarItem.badgeValue = @"new";//在小圖標的上面家字體加字體

  3) 設置選中的顏色

     分欄控制器.tabBar.tintColor
     self.tabBarController.tabBar.tintColor = [UIColor redColor]; 

  3) TabBar只能顯示五個tab Item,如果超過五個則會自動生成個Morede 標簽顯示剩余的Tab,這些Tab可以通過編輯顯示在UITabBar上(打開頁面后自動顯示在界面,點擊tabBar右邊)

  4) 自定義Item 

     [UITabBarItem alloc]initWithTitle: image: tag:

     [UITabBarItem alloc]initWithTabBarSystemItem:tag:
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,836評論 6 540
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,275評論 3 428
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,904評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,633評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,368評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,736評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,740評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,919評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,481評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,235評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,427評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,968評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,656評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,055評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,348評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,160評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,380評論 2 379

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