UITabBar

一、簡介

<<UITabBar類實(shí)現(xiàn)了一個(gè)選擇兩個(gè)或多個(gè)按鈕的控制,并稱之為項(xiàng)目。一個(gè)標(biāo)簽欄最常見的用途是實(shí)現(xiàn)一個(gè)模態(tài)接口攻一個(gè)項(xiàng)目的變化選擇。如果你想暫時(shí)突出或不會改變某個(gè)項(xiàng)目的外觀時(shí),輕按按鈕,使用一個(gè)UIToolbar對象。UITabBar類提供為用戶定制能力重新排列標(biāo)簽欄,刪除和酒吧中添加項(xiàng)目。您可以使用標(biāo)簽欄的委托,以增加這種行為。

<<UITabBar對象控制在不同的任務(wù)、試圖或模式間的切換,標(biāo)簽欄包含于標(biāo)簽欄控制器中,這是一個(gè)可以管理一系列自定義視圖顯示的程序?qū)ο蟆?一般和UITabBarConmutroller對象結(jié)合使用(也能獨(dú)立使用tab bars作為獨(dú)立控制)

<<一個(gè)UITabBarController只有一個(gè)TabBar。

<<繼承關(guān)系:UITabBar --> UIView?-->UIResponder-->NSObject

格式為

1--> 設(shè)置item位置的樣式(屬性的作用)

typedef NS_ENUM(NSInteger, UITabBarItemPositioning) {

? ? UITabBarItemPositioningAutomatic,

? ? UITabBarItemPositioningFill,

? ? UITabBarItemPositioningCentered,

} NS_ENUM_AVAILABLE_IOS(7_0);(如果屬性有枚舉類型的話,這里會有枚舉類型說明

tabBar.itemPositioning = UITabBarItemPositioningCentered;(這是具體的例子)

@property(nonatomic) UITabBarItemPositioning itemPositioning NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;(這是屬性的說明)

二、UITabBar的屬性(屬性的順序與蘋果API一致)

1-->聲明UITabBarDelegate代理

tabBar .delegate = self;//聲明代理

@property(nullable, nonatomic, weak) id <UITabBarDelegate>delegate; //設(shè)置UITabBarDelegate代理

三、UITabBar的標(biāo)簽屬性

1-->設(shè)置標(biāo)簽

tabBar.items = itemsArray。

@property(nullable, nonatomic, copy) NSArray<UITabBarItem *> *items;//get/set UITabBarItems,該數(shù)組必須包含的UITabBarItem對象 默認(rèn)是nil 改變時(shí)沒有動畫效果 按順序展示

2-->?設(shè)置選中的標(biāo)簽

???tabBar.selectedItem?=?tabBarItemArray;?//?設(shè)置tabBarItem為選中的標(biāo)簽

@property(nullable, nonatomic, weak) UITabBarItem *selectedItem; //顯示基于模式的反饋。默認(rèn)為空

3->?設(shè)置標(biāo)簽與動畫顯示

??? [tabBarController.tabBar setItems:tabBarItemArray animated:YES];

- (void)setItems:(nullable NSArray *)items animated:(BOOL)animated;?

四、UITabBar的自定義標(biāo)簽順序?qū)傩裕ㄗ⒁馐褂肬I TabBarController時(shí)不能使用此方法,自定義時(shí)使用)

1-->設(shè)置自定義標(biāo)簽

?[tabBar beginCustomizingItems:@[item1, item3, item2]];//讓用戶自定義items的布局,系統(tǒng)會自動彈出一個(gè)帶有Done按鈕的視圖

- (void)beginCustomizingItems:(NSArray *)items __TVOS_PROHIBITED; //重新排序。這將顯示一個(gè)包含所有條目的表單,允許用戶更改/重新排序項(xiàng)目,并在頂部顯示一個(gè)“Done”按鈕。列出所有可以重新排序的項(xiàng)目??偸前岩粡埣垊悠饋?。未列出的可見項(xiàng)是固定的。

2-->完成標(biāo)簽布局

這個(gè)方法博主是在找不到具體怎么調(diào)用的,只能貼出蘋果官方API-endCustomizingAnimated。希望有人知道的能告訴一下,來張老師的圖以安慰自己。


- (BOOL)endCustomizingAnimated:(BOOL)animated __TVOS_PROHIBITED;?

3-->是否正在自定義標(biāo)簽布局

BOOL customizing=imageView.customizing;?//獲取旋轉(zhuǎn)狀態(tài)

#if UIKIT_DEFINE_AS_PROPERTIES//UIKIT定義的屬性

@property(nonatomic, readonly, getter=isCustomizing) BOOL customizing __TVOS_PROHIBITED;

#else

- (BOOL)isCustomizing __TVOS_PROHIBITED;

#endif

五、UITabBar的顏色屬性

1-->設(shè)置渲染顏色,會影響選中字體和圖案的渲染

tabBar.tintColor=[UIColor redColor];

@property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(5_0);//tintColor是通過superview層次結(jié)構(gòu)繼承的

2-->?設(shè)置導(dǎo)航欄的顏色

tabBar.barTintColor = [UIColor whiteColor];

@property(nullable, nonatomic, strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;//?barTintColor會影響背景顏色, iOS7出現(xiàn)的新屬性,用來代替tintColor的作用

3-->未選中的Item的顏色 IOS10可用

tabBar.unselectedItemTintColor = [UIColor redColor];

@property (nonatomic, readwrite, copy, nullable) UIColor *unselectedItemTintColor NS_AVAILABLE_IOS(10_0) UI_APPEARANCE_SELECTOR;//在這個(gè)標(biāo)簽欄中未選中的項(xiàng)將被著色為這個(gè)顏色。將這個(gè)值設(shè)置為nil表明UITabBar應(yīng)該使用它的默認(rèn)值。

4-->選中的Item的Image顏色

tabBar.selectedImageTintColor = [UIColor redColor];

@property(nullable, nonatomic, strong) UIColor *selectedImageTintColor NS_DEPRECATED_IOS(5_0,8_0,"Use tintColor") UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;//棄用屬性,被tintColor代替

六、UITabBar的背景圖案屬性

1-->設(shè)置導(dǎo)航欄背景圖案

[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabBarBackgroundImage"]];

@property(nullable, nonatomic, strong) UIImage *backgroundImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;//即使不是通過UIImage resizableImage方法創(chuàng)建的,背景圖像也會被修改。

2-->?設(shè)置選中一個(gè)標(biāo)簽時(shí),標(biāo)簽背后的選中提示圖案?這個(gè)會出現(xiàn)在設(shè)置的item圖案的后面

tabBar.selectionIndicatorImage=[UIImage imageNamed:@"tabBarBackgroundImage"];

@property(nullable, nonatomic, strong) UIImage *selectionIndicatorImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

3-->設(shè)置陰影的背景圖案

[[UITabBarappearance] setShadowImage:[UIImagenew]];

@property(nullable, nonatomic, strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;//默認(rèn)是nil。當(dāng)非nil時(shí),自定義陰影圖像來顯示而不是默認(rèn)陰影圖像。對于要顯示的自定義陰影,還必須設(shè)置自定義背景圖像(如果使用默認(rèn)背景圖像,則使用默認(rèn)的陰影圖像)。

七、UITabBar的背景圖案屬性

1-->設(shè)置標(biāo)簽item的位置模式

//枚舉如下

typedef NS_ENUM(NSInteger,?UITabBarItemPositioning)?{

????UITabBarItemPositioningAutomatic,//自動

????UITabBarItemPositioningFill,//充滿

????UITabBarItemPositioningCentered,//中心

}?NS_ENUM_AVAILABLE_IOS(7_0);

[UITabBar appearance].itemPositioning=UITabBarItemPositioningCentered;

@property(nonatomic) UITabBarItemPositioning itemPositioning NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;//默認(rèn)是UITabBarItemPositioningAutomatic。標(biāo)簽欄項(xiàng)是水平填充的

2-->設(shè)置item寬度

[UITabBar appearance].itemWidth=50;

@property(nonatomic) CGFloat itemWidth NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;//設(shè)置值為0或小于0的值將被解釋為系統(tǒng)定義的寬度

3-->item間隙

[UITabBar appearance].itemSpacing=5;

@property(nonatomic) CGFloat itemSpacing NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;//設(shè)置值為0或小于0的值將被解釋為系統(tǒng)定義的間隔

4-->設(shè)置標(biāo)簽欄風(fēng)格,默認(rèn)高度49

typedef NS_ENUM(NSInteger, UIBarStyle) {

? ? UIBarStyleDefault? ? ? ? ? = 0,//默認(rèn)樣式

? ? UIBarStyleBlack? ? ? ? ? ? = 1,//黑色

? ? UIBarStyleBlackOpaque? ? ? = 1, // 棄用屬性

? ? UIBarStyleBlackTranslucent = 2, //?棄用屬性

} __TVOS_PROHIBITED;

tabBar.barStyle =UIBarStyleBlack;

@property(nonatomic) UIBarStyle barStyle NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR __TVOS_PROHIBITED;//UIBarStyleDefault和UIBarStyleBlack來定義UINavigationBar樣式,并且用setTranslucent:方法來設(shè)置透明與否

4-->設(shè)置item是否透明

[UITabBar appearance].translucent =NO;

@property(nonatomic,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(7_0);//設(shè)置tabBar的半透明屬性translucent設(shè)置為NO,默認(rèn)為YES,若保留半透明效果,設(shè)置的顏色會與正常的顏色有色差

八、UITabBar的UITabBarDelegate

1、用戶選中某個(gè)UITabBarItem

- (void)tabBar:(UITabBar*)tabBar didSelectItem:(UITabBarItem*)item{

// 判斷本次點(diǎn)擊的UITabBarItem是否和上次的一樣

if(item ==self.lastItem) {

// 一樣就發(fā)出通知

[[NSNotificationCenterdefaultCenter] postNotificationName:@"LLTabBarDidClickNotification"object:niluserInfo:nil];

?}

// 將這次點(diǎn)擊的UITabBarItem賦值給屬性

self.lastItem = item;

}

- (void)tabBar:(UITabBar*)tabBar didSelectItem:(UITabBarItem*)item;//用戶選擇新視圖時(shí)調(diào)用

標(biāo)注:以下四個(gè)代理方法是當(dāng)Items>=6個(gè)時(shí),當(dāng)進(jìn)入More頁面時(shí),開始或結(jié)束Item編輯狀態(tài)的相關(guān)回調(diào)

2、將要開始編輯標(biāo)簽時(shí)

- (void)tabBar:(UITabBar*)tabBar willBeginCustomizingItems:(NSArray *)items{

NSLog(@"將要開始自定制item");

}

- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray *)items __TVOS_PROHIBITED; //在自定義表顯示之前調(diào)用。標(biāo)簽是標(biāo)簽項(xiàng)目列表

3、已經(jīng)開始編輯標(biāo)簽時(shí)? ? ? ??

- (void)tabBar:(UITabBar*)tabBar didBeginCustomizingItems:(NSArray?*)items{

NSLog(@"已經(jīng)開始自定制item");

}

- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray *)items __TVOS_PROHIBITED; // 自定義表顯示后調(diào)用。標(biāo)簽是標(biāo)簽項(xiàng)目列表

4、將要結(jié)束自定制item ? ??

- (void)tabBar:(UITabBar*)tabBar willEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {

NSLog(@"將要結(jié)束自定制item");

}

- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray *)items changed:(BOOL)changed __TVOS_PROHIBITED; //在自定義表隱藏之前調(diào)用。標(biāo)簽是標(biāo)簽項(xiàng)目列表。

5、已經(jīng)結(jié)束自定制item ? ? ??

- (void)tabBar:(UITabBar*)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed{

NSLog(@"將要結(jié)束自定制item");

}

- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed __TVOS_PROHIBITED; //自定義表被隱藏后調(diào)用。標(biāo)簽是標(biāo)簽項(xiàng)目列表。

參考

iOS開發(fā)中 UITabBarController--標(biāo)簽控制器的使用

UINavigationBar-使用總結(jié)

iOS開發(fā)中 UITabBarController--標(biāo)簽控制器的使用

[ios]tabbar超過5個(gè)后出現(xiàn)的more標(biāo)簽文字修改 以及more、configure界面 修改問題

iOS Tabbar各種定制

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

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