一、簡介
<<UITabBar類實現了一個選擇兩個或多個按鈕的控制,并稱之為項目。一個標簽欄最常見的用途是實現一個模態接口攻一個項目的變化選擇。如果你想暫時突出或不會改變某個項目的外觀時,輕按按鈕,使用一個UIToolbar對象。UITabBar類提供為用戶定制能力重新排列標簽欄,刪除和酒吧中添加項目。您可以使用標簽欄的委托,以增加這種行為。
<<UITabBar對象控制在不同的任務、試圖或模式間的切換,標簽欄包含于標簽欄控制器中,這是一個可以管理一系列自定義視圖顯示的程序對象。 一般和UITabBarConmutroller對象結合使用(也能獨立使用tab bars作為獨立控制)
<<一個UITabBarController只有一個TabBar。
<<繼承關系:UITabBar --> UIView?-->UIResponder-->NSObject
格式為
1--> 設置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; //設置UITabBarDelegate代理
三、UITabBar的標簽屬性
1-->設置標簽
tabBar.items = itemsArray。
@property(nullable, nonatomic, copy) NSArray<UITabBarItem *> *items;//get/set UITabBarItems,該數組必須包含的UITabBarItem對象 默認是nil 改變時沒有動畫效果 按順序展示
2-->?設置選中的標簽
???tabBar.selectedItem?=?tabBarItemArray;?//?設置tabBarItem為選中的標簽
@property(nullable, nonatomic, weak) UITabBarItem *selectedItem; //顯示基于模式的反饋。默認為空
3->?設置標簽與動畫顯示
??? [tabBarController.tabBar setItems:tabBarItemArray animated:YES];
- (void)setItems:(nullable NSArray *)items animated:(BOOL)animated;?
四、UITabBar的自定義標簽順序屬性(注意使用UI TabBarController時不能使用此方法,自定義時使用)
1-->設置自定義標簽
?[tabBar beginCustomizingItems:@[item1, item3, item2]];//讓用戶自定義items的布局,系統會自動彈出一個帶有Done按鈕的視圖
- (void)beginCustomizingItems:(NSArray *)items __TVOS_PROHIBITED; //重新排序。這將顯示一個包含所有條目的表單,允許用戶更改/重新排序項目,并在頂部顯示一個“Done”按鈕。列出所有可以重新排序的項目。總是把一張紙動起來。未列出的可見項是固定的。
2-->完成標簽布局
這個方法博主是在找不到具體怎么調用的,只能貼出蘋果官方API-endCustomizingAnimated。希望有人知道的能告訴一下,來張老師的圖以安慰自己。
- (BOOL)endCustomizingAnimated:(BOOL)animated __TVOS_PROHIBITED;?
3-->是否正在自定義標簽布局
BOOL customizing=imageView.customizing;?//獲取旋轉狀態
#if UIKIT_DEFINE_AS_PROPERTIES//UIKIT定義的屬性
@property(nonatomic, readonly, getter=isCustomizing) BOOL customizing __TVOS_PROHIBITED;
#else
- (BOOL)isCustomizing __TVOS_PROHIBITED;
#endif
五、UITabBar的顏色屬性
1-->設置渲染顏色,會影響選中字體和圖案的渲染
tabBar.tintColor=[UIColor redColor];
@property(null_resettable, nonatomic, strong) UIColor *tintColor NS_AVAILABLE_IOS(5_0);//tintColor是通過superview層次結構繼承的
2-->?設置導航欄的顏色
tabBar.barTintColor = [UIColor whiteColor];
@property(nullable, nonatomic, strong) UIColor *barTintColor NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;//?barTintColor會影響背景顏色, iOS7出現的新屬性,用來代替tintColor的作用
3-->未選中的Item的顏色 IOS10可用
tabBar.unselectedItemTintColor = [UIColor redColor];
@property (nonatomic, readwrite, copy, nullable) UIColor *unselectedItemTintColor NS_AVAILABLE_IOS(10_0) UI_APPEARANCE_SELECTOR;//在這個標簽欄中未選中的項將被著色為這個顏色。將這個值設置為nil表明UITabBar應該使用它的默認值。
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-->設置導航欄背景圖案
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabBarBackgroundImage"]];
@property(nullable, nonatomic, strong) UIImage *backgroundImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;//即使不是通過UIImage resizableImage方法創建的,背景圖像也會被修改。
2-->?設置選中一個標簽時,標簽背后的選中提示圖案?這個會出現在設置的item圖案的后面
tabBar.selectionIndicatorImage=[UIImage imageNamed:@"tabBarBackgroundImage"];
@property(nullable, nonatomic, strong) UIImage *selectionIndicatorImage NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
3-->設置陰影的背景圖案
[[UITabBarappearance] setShadowImage:[UIImagenew]];
@property(nullable, nonatomic, strong) UIImage *shadowImage NS_AVAILABLE_IOS(6_0) UI_APPEARANCE_SELECTOR;//默認是nil。當非nil時,自定義陰影圖像來顯示而不是默認陰影圖像。對于要顯示的自定義陰影,還必須設置自定義背景圖像(如果使用默認背景圖像,則使用默認的陰影圖像)。
七、UITabBar的背景圖案屬性
1-->設置標簽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;//默認是UITabBarItemPositioningAutomatic。標簽欄項是水平填充的
2-->設置item寬度
[UITabBar appearance].itemWidth=50;
@property(nonatomic) CGFloat itemWidth NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;//設置值為0或小于0的值將被解釋為系統定義的寬度
3-->item間隙
[UITabBar appearance].itemSpacing=5;
@property(nonatomic) CGFloat itemSpacing NS_AVAILABLE_IOS(7_0) UI_APPEARANCE_SELECTOR;//設置值為0或小于0的值將被解釋為系統定義的間隔
4-->設置標簽欄風格,默認高度49
typedef NS_ENUM(NSInteger, UIBarStyle) {
? ? UIBarStyleDefault? ? ? ? ? = 0,//默認樣式
? ? 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:方法來設置透明與否
4-->設置item是否透明
[UITabBar appearance].translucent =NO;
@property(nonatomic,getter=isTranslucent) BOOL translucent NS_AVAILABLE_IOS(7_0);//設置tabBar的半透明屬性translucent設置為NO,默認為YES,若保留半透明效果,設置的顏色會與正常的顏色有色差
八、UITabBar的UITabBarDelegate
1、用戶選中某個UITabBarItem
- (void)tabBar:(UITabBar*)tabBar didSelectItem:(UITabBarItem*)item{
// 判斷本次點擊的UITabBarItem是否和上次的一樣
if(item ==self.lastItem) {
// 一樣就發出通知
[[NSNotificationCenterdefaultCenter] postNotificationName:@"LLTabBarDidClickNotification"object:niluserInfo:nil];
?}
// 將這次點擊的UITabBarItem賦值給屬性
self.lastItem = item;
}
- (void)tabBar:(UITabBar*)tabBar didSelectItem:(UITabBarItem*)item;//用戶選擇新視圖時調用
標注:以下四個代理方法是當Items>=6個時,當進入More頁面時,開始或結束Item編輯狀態的相關回調
2、將要開始編輯標簽時
- (void)tabBar:(UITabBar*)tabBar willBeginCustomizingItems:(NSArray *)items{
NSLog(@"將要開始自定制item");
}
- (void)tabBar:(UITabBar *)tabBar willBeginCustomizingItems:(NSArray *)items __TVOS_PROHIBITED; //在自定義表顯示之前調用。標簽是標簽項目列表
3、已經開始編輯標簽時? ? ? ??
- (void)tabBar:(UITabBar*)tabBar didBeginCustomizingItems:(NSArray?*)items{
NSLog(@"已經開始自定制item");
}
- (void)tabBar:(UITabBar *)tabBar didBeginCustomizingItems:(NSArray *)items __TVOS_PROHIBITED; // 自定義表顯示后調用。標簽是標簽項目列表
4、將要結束自定制item ? ??
- (void)tabBar:(UITabBar*)tabBar willEndCustomizingItems:(NSArray *)items changed:(BOOL)changed {
NSLog(@"將要結束自定制item");
}
- (void)tabBar:(UITabBar *)tabBar willEndCustomizingItems:(NSArray *)items changed:(BOOL)changed __TVOS_PROHIBITED; //在自定義表隱藏之前調用。標簽是標簽項目列表。
5、已經結束自定制item ? ? ??
- (void)tabBar:(UITabBar*)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed{
NSLog(@"將要結束自定制item");
}
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray *)items changed:(BOOL)changed __TVOS_PROHIBITED; //自定義表被隱藏后調用。標簽是標簽項目列表。
參考
iOS開發中 UITabBarController--標簽控制器的使用
iOS開發中 UITabBarController--標簽控制器的使用